I’ve got a model with a unique_together constraint.
class Postit(models.Model):
"""Represents a single post-it."""
x_axis = models.PositiveIntegerField(_('X axis'))
y_axis = models.PositiveIntegerField(_('Y axis'))
content = models.CharField(_('Content'), max_length=140, default='')
class Meta:
unique_together = ('x_axis', 'y_axis')
If I use a form to create a new post-it, the constraint is checked, and in case of a conflict, the error is listed in the non_field_errors property. Fine.
My problem is that I want to launch a different action depending of the kind of form error. I want a specific action if there is a unique constraint error, and another action for any other kind of errors.
Given that my app will be translated in several languages, how do I know if the form is invalid because of the constraint or for another reason?
Bear in mind that you can translate the string you’re comparing if you want to just check
non_field_errors.That’s not really the most elegant solution though. Your best bet is to simply verify the
unique_togetherconstraint yourself inModel.cleanorModel.validate_uniqueand respond accordingly.