Home Ask Login Register

Developers Planet

Your answer is one click away!

Thaian February 2016

Django forms - Select a valid choice. That choice is not one of the available choices

In my form I have select box but when after submit i getting error on this field: "Select a valid choice. That choice is not one of the available choices." I'm using exactly code in other form and there everything works fine but not in this form :(


BLANK_CHOICE = (('', '---------'),)

class ClientCreateForm(forms.ModelForm):
    class Meta:
        model = Client
        fields = ('name', 'address', 'zip_code', 'city', 'country', 'forwarding_address',
                  'forwarding_zip_code', 'forwarding_city', 'forwarding_country')

    def __init__(self, *args, **kwargs):
        super(ClientCreateForm, self).__init__(*args, **kwargs)
        self.fields['country'].choices = CountriesShortcut.objects.all().values_list('id', 'name')
        self.fields['forwarding_country'].choices = BLANK_CHOICE + tuple(
            CountriesShortcut.objects.all().values_list('id', 'name'))


class CountriesShortcut(models.Model):
    name = models.CharField(max_length=80, unique=True)

    class Meta:
        ordering = ['id']

    def __init__(self):

class Client(models.Model):
    id = models.OneToOneField(User, on_delete=models.CASCADE, unique=True, primary_key=True)
    name = models.CharField(max_length=256, unique=True)
    address = models.CharField(max_length=64)
    zip_code = models.CharField(max_length=10, help_text='Zip Code')
    city = models.CharField(max_length=64)
    country = models.ForeignKey(CountriesShortcut, related_name='country', blank=True, null=True)
    forwarding_address = models.CharField(max_length=64, blank=True)
    forwarding_zip_code = models.CharField(max_length=10, blank=True)
    forwarding_city = models.CharField(max_length=64, blank=True)
    forwarding_country = models.ForeignKey(CountriesShortcut, related_name='forwarding_country', blank=True, null=True)

    def __str__(self):
        re = self.name + ' [' +        


Karolis Ryselis February 2016

Your fields are foreign keys. By default django sets all available values for fields in forms to all objects of the model. Looking at your code, I think you could just remove ClientCreateForm __init__ completely.

If you still want to set your custom choices, you should not set choices attribute. You should use queryset:

class ClientCreateForm(forms.ModelForm):
# your meta goes here

    def __init__(self, *args, **kwargs):
        super(ClientCreateForm, self).__init__(*args, **kwargs)
        self.fields['country'].queryset = CountriesShortcut.objects.all()
        self.fields['forwarding_country'].queryset = CountriesShortcut.objects.all()

Your fields are marked as null=True, so empty dash will be added automatically.

choices should be used if you have non-relationship field, for example, CharField with choices attribute set.

Post Status

Asked in February 2016
Viewed 2,494 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life