92👍
✅
following from comments. Probably yes:
class ThatForm(ModelForm):
def __init__(self, *args, **kwargs):
# first call parent's constructor
super(ThatForm, self).__init__(*args, **kwargs)
# there's a `fields` property now
self.fields['desired_field_name'].required = False
49👍
you ought to add blank=True
to the corresponding model
If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True.
Also see the documentation for blank itself.
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-Are sessions needed for python-social-auth
- [Django]-What is the clean way to unittest FileField in django?
34👍
When we need to set required option on a bunch of fields we can:
class ThatForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.Meta.required:
self.fields[field].required = True
class Meta:
model = User
fields = (
'email',
'first_name',
'last_name',
'address',
'postcode',
'city',
'state',
'country',
'company',
'tax_id',
'website',
'service_notifications',
)
required = (
'email',
'first_name',
'last_name',
'address',
'postcode',
'city',
'country',
)
- [Django]-What is the equivalent of "none" in django templates?
- [Django]-Is it possible to generate django models from the database?
- [Django]-Django CSRF check failing with an Ajax POST request
5👍
the following may be suitable
class ThatForm(ModelForm):
text = forms.CharField(required=False, widget=forms.Textarea)
👤quin
- [Django]-Django – Render the <label> of a single form field
- [Django]-Django – get() returned more than one topic
- [Django]-Class Based Views VS Function Based Views
-16👍
You could try this:
class ThatForm(ModelForm):
class Meta:
requireds =
{
'text':False,
}
requireds must be under Meta.
- [Django]-Generate unique id in django from a model field
- [Django]-Duplicate column name
- [Django]-How does one make logging color in Django/Google App Engine?
Source:stackexchange.com