Hi,
I just tried the new Model Validation feature in Django 1.2 and I
found out that ModelForm always calls the Model's clean method even
when the form validation has errors. This causes problems when the
model has a foreign key, and the value is validated in the clean
method. Consider this example:
class Flight(models.Model):
aircraft = models.ForeignKey(Aircraft)
departure = models.ForeignKey(Departure)
arrival = models.ForeignKey(Arrival)
def clean(self):
# There can never be flights departing and arriving to the
same place
if self.departure == self.arrival:
raise ValidationError("Departure is the same as Arrival")
class FligthForm(forms.ModelForm):
class Meta:
model = Flight
If the form is submitted with empty values, I will get a DoesNotExist
exception when trying to access the self.departure/self.arrival
attribute in the clean method. Is this by design? If it is then what
is the recommended practice to implement the Model's clean method?
Regards,
Rendy Anthony (ak37)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
django...@googlegroups.com.
To unsubscribe from this group, send email to
django-users...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.