Question

I'm trying to set the Gender for my Users in Admin but get the Error

Value u"[u'm']" is not a valid choice.

admin.py

class PlayerForm(forms.ModelForm):
    GENDER_CHOICES = (
        ('m', 'Male'),
        ('f', 'Female'),
    )
...
gender = forms.MultipleChoiceField(label="Gender", choices=GENDER_CHOICES)
...

class Meta:
    model = Player


class PlayerAdmin(admin.ModelAdmin):
    form = PlayerForm


admin.site.register(Player, PlayerAdmin)

models.py (Player Model)

class Player(AbstractBaseUser):
    GENDER_CHOICES = (
        ('m', 'Male'),
        ('f', 'Female'),
    )
    ...
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    ...

Fun fact: The form does display the correct default value from dadabase in admin.

POST Payload when trying to submit the Form:

------WebKitFormBoundary009tVyo4cRJvIAnC
Content-Disposition: form-data; name="gender"

m

Edit: Forgot to mention that the DB is Postgres 8.4 and the column in question is a

VARCHAR(1)
Was it helpful?

Solution

Since you are using MultipleChoiceField:

gender = forms.MultipleChoiceField(label="Gender", choices=GENDER_CHOICES)

It's going to save gender to a "list". Resulting in:

>>> gender = ['m']

You need to use ChoiceField which is used to select a single thing:

gender = forms.ChoiceField(label="Gender", choices=GENDER_CHOICES)

Resulting in:

>>> gender = 'm'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top