Django: __init__() got an unexpected keyword argument 'choices'

10,673

Form fields like your mobile_no for example don't have a unique argument. Only model fields have that. You have to remove all unique arguments from your form model.

Share:
10,673
Manas Chaturvedi
Author by

Manas Chaturvedi

Software Engineer 2 at Haptik

Updated on June 04, 2022

Comments

  • Manas Chaturvedi
    Manas Chaturvedi almost 2 years

    I'm trying to create a form, and pass on the values entered by the use into a database.

    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})
    

    However, I'm encountering the following error:

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

    What seems to be wrong with my code? And what does this error signify?

    EDIT:

    I've modified the source code of my forms.py file, and also made a slight chance in the original title. Seems like the choices key in the parameters on my form fields is generating an error. However, the code is syntactically correct, right?

  • Manas Chaturvedi
    Manas Chaturvedi almost 9 years
    I have edited the question and the original post as well based on changing my code on your suggestions.
  • Klaus D.
    Klaus D. almost 9 years
    This time you used the wrong field type one other_social_accounts it should be a ChoiceField.
  • Klaus D.
    Klaus D. almost 9 years
    Some other fields as well, check docs.djangoproject.com/en/1.8/ref/forms/fields if the field type you are using have a choices argument.
  • Daniel Roseman
    Daniel Roseman almost 9 years
    Alternatively, use ChoiceField rather than CharField.