#35910: Use violation_error_message on UniqueContraint when only fields and no
constraint is provided
-------------------------------------+-------------------------------------
     Reporter:  James Walters        |                     Type:  New
                                     |  feature
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:                       |                 Severity:  Normal
     Keywords:  forms, models,       |             Triage Stage:
  unique constraints                 |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 I have a model with the following UniqueConstraint:

 {{{
 models.UniqueConstraint(
   fields=["client", "location", "description", "affects_all_locations"],
   name="unique_desc_per_location_per_client",
   violation_error_message="A closure with this description already exists
 for this location (or all locations).",
 )
 }}}

 Instead of seeing the error message I provided, I get "Closure with this
 Client, Location, Description and Affects all locations already exists."
 Upon consulting the
 [https://docs.djangoproject.com/en/5.1/ref/models/constraints/#id3
 documentation], I see that this is actually the intended behavior:

      This message is not used for UniqueConstraints with fields and
 without a condition. Such UniqueConstraints show the same message as
 constraints defined
      with Field.unique or in Meta.unique_together.

 The default error message, however, gives the names of model fields that
 users know nothing about, and the message I'm trying to provide gives the
 user a better explanation about what's going on. Currently, I would have
 to override the form's own validation to show this error message. However,
 I don't think this should be necessary, given that I can provide a
 violation_error_message straight to the UniqueConstraint.

 I'm filing this under models/ORM since this is specifically related to
 model validation, though reclassify it as Forms if that turns out to be
 more appropriate.

 If we decide we want to go in this direction, I'm open to working on a
 patch, but I'll wait to see if we agree that this behavior should be
 changed.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35910>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019326fbb934-72a84d7f-b553-4a0b-a852-2a93ef808a33-000000%40eu-central-1.amazonses.com.

Reply via email to