Issue
I have a form like this:
class My_Form(ModelForm):
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
How can I set the address field as optional?
Solution
Guess your model is like this:
class My_Class(models.Model):
address = models.CharField()
Your form for Django version < 1.8:
class My_Form(ModelForm):
address = forms.CharField(required=False)
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
Your form for Django version > 1.8:
class My_Form(ModelForm):
address = forms.CharField(blank=True)
class Meta:
model = My_Class
fields = ('first_name', 'last_name' , 'address')
Shameless plug: Check your Python knowledge at http://python.softwarequizzes.com
Answered By - Akshar Raaj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.