Hi,
It's the first time I'm playing with model validation, and I'm a bit
stuck with something. I couldn't find any help on this mailinglist or
in the doc, but if there is please let me know where :)
Basically I'd like to validate a model field in the context of its
instance. First I assumed it would work like with form validation, so
I did:
class Title(models.Model):
is_video = models.BooleanField(...)
aspect_ratio = models.CharField(...)
...
clean_aspect_ratio(self, value):
if self.is_video is True and value == '':
raise ValidationError('The aspect ratio is required for
video titles.')
But this doesn't work. The 'clean_aspect_ratio' method is never
called. I could use validators, but from there I can't have access to
the model instance and therefore I can't access the 'is_video'
attribute. It sort of works with the 'clean' method:
class Title(models.Model):
...
clean(self):
if self.is_video and value == '':
raise ValidationError('The aspect ratio is required for
video titles.')
However, this error is not attached specifically to the 'aspect_ratio'
attribute, therefore the error is displayed at the top of the admin
form rather than next to the aspect_ratio form field.
Is there a way to achieve the same type of specific validation like
with forms?
Many thanks,
Julien
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.