Django:__ init __()得到了一个意想不到的关键字参数'选择'

时间:2015-06-24 19:16:41

标签: python django python-2.7

我尝试创建表单,并将use输入的值传递给数据库。

models.py

from django.db import models

class Evangelized(models.Model):
    full_name = models.CharField(max_length = 128)
    email = models.EmailField()
    mobile_no = models.CharField(unique = True, max_length = 128)
    twitter_url = models.CharField(unique = True, max_length = 128)
    GENDER_CHOICES = (('M', 'Male'), ('F', 'Female'), ('U', 'Unisex/Parody'))
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    AREA_CHOICES = (('Govt', 'Govt'), ('Entertainment', 'Entertainment'), ('Automobile', 'Automobile'),
                        ('Careers', 'Careers'), ('Books', 'Books'), ('Family', 'Family'), ('Food', 'Food'),
                            ('Gaming', 'Gaming'), ('Beauty', 'Beauty'), ('Sports', 'Sports'), ('Events', 'Events'),
                                ('Business', 'Business'), ('Travel', 'Travel'), ('Health', 'Health'), ('Technology','Technology'))
    area_of_interest = models.CharField(choices = AREA_CHOICES, max_length = 128)
    other_area_of_interest = models.CharField(blank = True, max_length = 128)
    city = models.CharField(max_length = 128)
    BOOL_CHOICES = ((True, 'Yes'), (False, 'No'))
    other_social_accounts = models.BooleanField(choices = BOOL_CHOICES, default = None)
    questions = models.CharField(blank = True, max_length = 1280)
    about_yourself = models.CharField(blank = False, max_length = 1280)
    referrer = models.CharField(max_length = 128)

forms.py

   from django import forms
from rango.models import Evangelized

class EvangelizedForm(forms.ModelForm):
    full_name = forms.CharField(help_text="Full Name")
    email = forms.CharField(help_text="Email ID")
    mobile_no = forms.CharField(help_text="Mobile number")
    twitter_url = forms.CharField(help_text="Twitter URL")
    gender = forms.ChoiceField(widget=forms.RadioSelect(), 
                 choices=Evangelized.GENDER_CHOICES, help_text="Gender")
    area_of_interest = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(),
                                                    choices=Evangelized.AREA_CHOICES, help_text="Areas of interest(Upto 3)")
    """def clean_area_of_interest(self):
        if len(self.cleaned_data['area_of_interest']) > 3:
            raise forms.ValidationError('Select no more than 3.')
        return self.cleaned_data['area_of_interest']"""
    other_area_of_interest = forms.CharField(help_text="Other area of interest")
    city = forms.CharField(help_text="City")
    other_social_accounts = forms.CharField(widget=forms.RadioSelect(),choices=Evangelized.BOOL_CHOICES, help_text="Do you have other social accounts?", max_length=128)
    questions = forms.CharField(help_text="Do you have any questions?")
    about_yourself = forms.CharField(help_text="Tell us about yourself")
    referrer = forms.CharField(help_text="I heard about this platform via:")

    class Meta:
        model = Evangelized
        fields = ('full_name', 'email', 'mobile_no', 'twitter_url', 'gender', 'area_of_interest', 'other_area_of_interest', 'city', 'other_social_accounts',
                    'questions', 'about_yourself', 'referrer')

views.py

def fillform(request):
    if request.method == 'POST':
        form = EvangelizedForm(request.POST)
        if form.is_valid():
            form.save(commit = True)
            return index(request)
        else:
            form.errors
    else:
        form = EvangelizedForm()

    return render(request, 'rango/fillform.html', {'form': form})

但是,我遇到以下错误:

Exception Type: TypeError at /rango/
Exception Value: __init__() got an unexpected keyword argument 'unique'

我的代码似乎有什么问题?这个错误意味着什么?

编辑:

我修改了forms.py文件的源代码,并且在原始标题中也略有机会。似乎我的表单字段中的参数中的choices键生成错误。但是,代码在语法上是正确的,对吧?

2 个答案:

答案 0 :(得分:0)

mobile_no之类的表单字段例如没有unique参数。只有模型字段才有。您必须从表单模型中删除所有unique个参数。

答案 1 :(得分:0)

我已经弄清楚是什么导致了这个错误。 choices参数应该在forms.py中的小部件构造函数中定义,如下所示:

from django import forms
from rango.models import Evangelized

class EvangelizedForm(forms.ModelForm):
    full_name = forms.CharField(help_text="Full Name")
    email = forms.CharField(help_text="Email ID")
    mobile_no = forms.CharField(help_text="Mobile number")
    twitter_url = forms.CharField(help_text="Twitter URL")
    gender = forms.ChoiceField(widget=forms.RadioSelect(
                 choices=Evangelized.GENDER_CHOICES), help_text="Gender")
    area_of_interest = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(
                                                    choices=Evangelized.AREA_CHOICES), help_text="Areas of interest(Upto 3)")
    """def clean_area_of_interest(self):
        if len(self.cleaned_data['area_of_interest']) > 3:
            raise forms.ValidationError('Select no more than 3.')
        return self.cleaned_data['area_of_interest']"""
    other_area_of_interest = forms.CharField(help_text="Other area of interest")
    city = forms.CharField(help_text="City")
    other_social_accounts = forms.CharField(widget=forms.RadioSelect(choices=Evangelized.BOOL_CHOICES), help_text="Do you have other social accounts?", max_length=128)
    questions = forms.CharField(help_text="Do you have any questions?")
    about_yourself = forms.CharField(help_text="Tell us about yourself")
    referrer = forms.CharField(help_text="I heard about this platform via:")

    class Meta:
        model = Evangelized
        fields = ('full_name', 'email', 'mobile_no', 'twitter_url', 'gender', 'area_of_interest', 'other_area_of_interest', 'city', 'other_social_accounts',
                    'questions', 'about_yourself', 'referrer')