Say I have an address
table and it has a postal_code
field -- ModelChoiceField does not allow me to use something other than PKs to validate existence
If postal_code
is a foreign key to a PostalCode model that contains valid postal codes I would just use use a CharField and then do a clean like you suggested. My clean method would look like this:
def clean_postal_code(self):
try:
code = PostalCode.objects.get(code_field=self.data['postal_code'])
except:
raise forms.ValidationError("Please enter a valid postal code")
return code