Opened 21 months ago

Closed 21 months ago

Last modified 21 months ago

#34338 closed New feature (fixed)

Allow to customize the code attribute of ValidationError raised by BaseConstraint.validate

Reported by: xafer Owned by: xafer
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Gagaro Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It is currently possible to customize the violation_error_message of a ValidationError raised by a constraint but not the code.

I'd like to add a new violation_error_message parameter to BaseConstraint to allow to easily add one.

Currently, to achieve the same result, you have to subclass the constraint to tweak validate to catch and reraise the ValidationError.

Since the documentation recommends to Provide a descriptive error code to the constructor: when raising a ValidationError in https://docs.djangoproject.com/en/4.1/ref/forms/validation/#raising-validationerror , I think it would make sense to provide this possibility for errors raised by constraints.

If you think it would be a good idea, I'd be happy to work on a PR.

Change History (6)

in reply to:  description comment:1 by Mariusz Felisiak, 21 months ago

Cc: Gagaro added
Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted

Replying to xafer:

It is currently possible to customize the violation_error_message of a ValidationError raised by a constraint but not the code.

I'd like to add a new violation_error_message parameter to BaseConstraint to allow to easily add one.

Agreed, adding violation_error_code sounds like a good idea as we have a similar mechanism for validators.

comment:2 by xafer, 21 months ago

Owner: changed from nobody to xafer
Status: newassigned

comment:3 by xafer, 21 months ago

Has patch: set

comment:4 by Mariusz Felisiak, 21 months ago

Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 21 months ago

Resolution: fixed
Status: assignedclosed

In 5b3d3e4:

Fixed #34338 -- Allowed customizing code of ValidationError in BaseConstraint and subclasses.

comment:6 by GitHub <noreply@…>, 21 months ago

In 2c428b3:

Refs #34338 -- Fixed isolation of constraints tests.

Regression in 5b3d3e400ab9334ba429ca360c9818c6dfc3a51b.

Note: See TracTickets for help on using tickets.
Back to Top