具有在__init__中具有参数的ModelForms的表单向导

具有在__init__中具有参数的ModelForms的表单向导

问题描述:

我在Ubuntu 14.04上使用python 2.7,Django 1.9.4.

I am using python 2.7, Django 1.9.4 on Ubuntu 14.04.

我已经为django-formtools(特别是表单向导)苦苦挣扎了好几天了.情况如下:

I have been struggling with django-formtools (specifically form wizard) for quite a few days now. The scenario is as follows:

表单向导只是一个两步过程:

The form wizard is just a 2 step process:

  1. 第一步:我有一个基于模型的ModelForm.表单的 __ init()__ 需要一个参数(已登录用户的ID,它是一个整数)
  2. 第二步:询问用户他/她想要提交表单的简单复选框.
  1. 1st step: I have a ModelForm based on a Model. The form's __init()__ requires a parameter (the id of logged in user which is an integer)
  2. 2nd step: A simple check box that asks user of he/she wants to submit the form.

forms.py的来源:

The source for forms.py:

from django import forms
from publishermanagement import models
from localemanagement import models as locale_models
from usermanagement import models as user_models


class AddPublisherForm(forms.ModelForm):
def __init__(self, user_id, *args, **kwargs):
    super(AddPublisherForm, self).__init__(*args, **kwargs)
    permitted_locale_ids = (
        user_models
        .PublisherPermission
        .objects
        .filter(user=user_id)
        .values_list('locale', flat=True))
    self.fields['locale'].queryset = (
        locale_models
        .Locale
        .objects
        .filter(pk__in=permitted_locale_ids))

class Meta:
    model = models.Information
    fields = (
        'channel_type',
        'current_deal_type',
        'locale',
        'name',
        'contact_primary',
        'contact_auxiliary',
        'website',
        'phone',
        'is_active',)


class ConfirmPublisherForm(forms.Form):
confirmation = forms.BooleanField(
    label="Check to confirm provided publisher data")

我按照包括堆栈溢出在内的各种论坛中的建议重写了 get_form_instance(). Information 是Model类,基于该类创建 AddPublisherForm .

I overwrote the get_form_instance() in line with the suggestions in various forums including Stack Overflow. Information is the Model class based on which AddPublisherForm is created.

views.py

from django.shortcuts import render_to_response
from django.contrib.auth.decorators import login_required
from publishermanagement import models as publisher_models
from formtools.wizard.views import SessionWizardView

class CreatePublisherWizard(SessionWizardView):

@login_required(login_url='/account/login/')
def done(self, form_list, **kwargs):
    # code for saving form data to be done here if user confirms the data
    # else redirect to the main form.
    return render_to_response(
        'publishermanagement/wiz.html',
        {'form_data': [form.cleaned_data for form in form_list]})

def get_form_instance(self, step):
    if step == u'0':

        info = publisher_models.Information()
        return info
    # the default implementation
    return self.instance_dict.get(step, None)

但是,在执行时,当我在Firefox中调用URL时,出现错误 __ init __()至少包含2个参数(给定1个).当我从forms.py中删除 __ init __()时,代码运行正常.

However, upon execution, when I call the URL in Firefox, I am getting the error __init__() takes at least 2 arguments (1 given). When I remove the __init__() from my forms.py, the code runs fine.

以及有关如何在django-formtools中创建此ModelForm的想法.

And ideas on how can create this ModelForm in django-formtools be integrated.

一些相关的帖子: WizardView Sublcass中的访问请求对象

您需要覆盖 查看更多