Hi folks, Please note this is partly an extension of some discussion in PR 16054 asking whether single-field unique or check constraints should raise field errors as opposed to non-field errors <https://github.com/django/django/pull/16054#pullrequestreview-1110802215>.
As part of Django's new constraint validation, constraints now get a violation_error_message <https://docs.djangoproject.com/en/4.1/ref/models/constraints/#violation-error-message> to declare custom user-friendly error messages. Currently, UniqueConstraints with a single field can additionally have form-specific error messages set via forms' error_messages <https://docs.djangoproject.com/en/4.1/topics/forms/modelforms/#considerations-regarding-model-s-error-messages> dictionary (or in model form fields). If it's decided that check constraints also raise field errors then they will also be specifically customisable in forms. I'd like to propose that constraints raising non-field errors to also be specifically customisable in addition to the general default set in violation_error_message – To do this validation errors would need the `code` kwarg set. One possibility is setting the code based on the *constraint name*. Single-field unique constraints already set the code "unique"; and this works for field errors because there's usually only one unique constraint defined for a field. Thoughts? Cheers, David -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CADyZw-7b5JoX1kyQvHPKr9pi_yMSNi3OzsbLtSUOXLC0j7MMvA%40mail.gmail.com.