2👍
In the Meta
class of form you can exclude fields:
class SearchForm(forms.Form):
# form fields here
class Meta:
exclude = ('location_area', 'final_date',)
If you do not want to exclude fields from form and still don’t want to validate them then write a custom field clean method for form which does nothing:
class SearchForm(forms.Form):
# form fields here
def clean_location_area(self):
location_area = self.cleaned_data['location_area']
return location_area
0👍
Basically you can override init method of your form:
Eg
class SearchForm(forms.Form):
# form fields here
def __init__(self, post_data=None, post_files=None):
if post_data and post_files:
self.base_fields.remove(field_name)
super(SearchForm, self).__init__(post_data, post_files)
else:
super(SearchForm, self).__init__()
So, basically while you get the form you can use: SearchForm()
and while you post data to the form you can use: SearchForm(request.POST, request.FILES)
.
In __init__
method we are checking whether the request is post or get using post_data
and post_files
. So, if it is post we are removing the field from base_field
so that it won’t check for validation for that field.
Tested in Django 1.11
- [Answered ]-Django: How to filter patients from one department?
- [Answered ]-Django: limit_choices_to with with a "through" intermediate table
- [Answered ]-Proper way of using url patterns
- [Answered ]-Password to be used with external service
- [Answered ]-Django and Rails with one common DB
Source:stackexchange.com