Dennis
Dennis

Reputation: 497

Django - Error - Select a valid choice. [<some choice>] is not one of the available choices

views.py

class AddLocationPageView(FormView):
    template_name = 'add_location.html'
    form_class = LocationForm
    success_url = '/add_location/location_added/'

    def form_valid(self, form):
        form.save()
        return super(AddLocationPageView, self).form_valid(form)

models.py

type_choices = (
    ('Рассвет/Закат', 'Рассвет/Закат'),('Ландшафт', 'Ландшафт'),('Природа', 'Природа'),
    ('Вода', 'Вода'),('Животные', 'Животные'),('Люди', 'Люди'),
    ('Архитектура', 'Архитектура'),('Город', 'Город'),('Астрофото', 'Астрофото'),
    ('Панорама', 'Панорама'),('Транспорт', 'Транспорт'),('Свадьба', 'Свадьба'),
)

visit_choices = (
    ('Январь', 'Январь'),('Февраль', 'Февраль'),('Март', 'Март'),
    ('Апрель', 'Апрель'),('Май', 'Май'),('Июнь', 'Июнь'),
    ('Июль', 'Июль'),('Август', 'Август'),('Сентябрь', 'Сентябрь'),
    ('Октябрь', 'Октябрь'),('Ноябрь', 'Ноябрь'),('Декабрь', 'Декабрь'),
)

class Location(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
    name = models.CharField(max_length=100, verbose_name="Локация", default='')
    types = models.CharField(max_length=50, verbose_name="Тип локации", choices=type_choices, default='')
    visit_times = models.CharField(max_length=50, verbose_name="Лучшее время для съемки", choices=visit_choices, default='')
    photos = models.ImageField(upload_to='photos', null=True, blank=True)
    keywords = models.CharField(max_length=100, verbose_name="Ключевые слова", default='')
    description = models.TextField(null=True, blank=True)

    def __unicode__(self):
        return self.name

forms.py

class LocationForm(forms.ModelForm):
    class Meta:
        model = Location
        fields = ['name', 'types', 'visit_times', 'photos', 'keywords', 'description']
        widgets = {
            'name': forms.TextInput(attrs={'placeholder': 'Напр. Стоунхендж'}),
            'types': forms.SelectMultiple(),
            'visit_times': forms.SelectMultiple(),
            'keywords': forms.TextInput(attrs={'placeholder': 'Напр. море, побережье, скалы'}),
            'description': forms.Textarea(attrs={'placeholder': 'Любая информация, которую посчитаете нужной'})
        }

I fill in the fields on the page and do some choice on SELECT fields. Then I push the button and get the error on SELECT fields --> Select a valid choice. [some choice] is not one of the available choices

Thanks a lot!

Upvotes: 0

Views: 1237

Answers (1)

trantu
trantu

Reputation: 1089

Try

    'types': forms.Select(),
    'visit_times': forms.Select(),

You defined in your model that types and also visit_times can have only one value from the choices. It is possible to get more choices but you have to think about how to save this data to database. Take a look at this:

https://pypi.python.org/pypi/django-multiselectfield/

https://pypi.python.org/pypi/django-select-multiple-field/

Upvotes: 1

Related Questions