Ok, so I have a model like this:
class Airplane(models.Model):
tail = models.ForeignKey(Tails)
wheel = models.CharField(max_length=500,blank=True)
window = models.CharField(max_length=500,blank=True)
def clean(self):
if self.tail and self.wheel and self.window:
raise ValidationError("Can't have all three, choose tail and one more")
Now if I go to add a new Airplane record using Django admin. If I leave both fields blank and save I get this Django error pointing to the line if self.tail and self.wheel.
DoesNotExist at /admin/MyProject/airplane/add/
Request Method: POST
Request URL: http://44.101.44.172:8001/admin/MyProject/airplane/add/
Django Version: 1.2.5
Exception Type: DoesNotExist
Exception Value:
Exception Location: /usr/lib/python2.7/site-packages/django/db/models/fields/related.py in __get__, line 299
Python Executable: /usr/bin/python
Python Version: 2.7.0
Shouldn’t Django be checking that the required fields are filled in before running clean? In any case, what’s the best way to handle this issue?
Ok, as a workaround I ended up defining a custom form with my validation in admin.py for this model.
I still don’t understand why model.clean gets called before Django has determined that the required fields have been filled in. Oh well.