How do I create a Django ModelForm, so that it's fields are sometimes required, sometimes not?
Posted
by Graf
on Stack Overflow
See other posts from Stack Overflow
or by Graf
Published on 2010-06-01T09:35:08Z
Indexed on
2010/06/01
9:43 UTC
Read the original article
Hit count: 258
Ok, here is the question. Imagine I have a ModelForm which have only two fields. like this one:
class ColorForm(forms.Form):
color_by_name = forms.CharField()
color = forms.IntegerField(widget = forms.Select(choices=COLOR_CHOICES))
So a user can either input a color name, a choose it from a list. Color is required, but that doesn't mean, that user should enter it manually. There do I put validation, so that my code checks if user selected color in dropdownlist and if not then he should write it manually?
© Stack Overflow or respective owner