[Answer]-Validate POST data with extra argument using Django modelForm

0👍

This worked in the end:

class SearchPreferenceForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(SearchPreferenceForm, self).__init__(*args, **kwargs)
        self.fields['location'].empty_values.append('all')
        self.fields['school_type'].empty_values.append('all')

1👍

Your model needs blank=True as well and null=True

location = models.ForeignKey(Location, blank=True, null=True)
school_type = models.ForeignKey(SchoolType, blank=True, null=True)

This post talks about blank and null.

Leave a comment