Opened 4 years ago

Closed 2 years ago

#15616 closed Cleanup/optimization (needsinfo)

django.core.exceptions.ValidationError is poorly implemented and documented

Reported by: tomevans223 Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords: validation validationerror
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

django.core.exceptions.ValidationError has a horrific interface. It's publicly accessible attributes differ depending upon how the object is created, as do the pre/post conditions for it's publicly accessible methods.

If constructed with a dictionary of error messages, then it gets an attribute 'message_dict'.
If constructed with a string, or a list of strings, then it gets an attribute 'messages'.
The documentation on Model.clean() talks about ValidationError, but does not mention this at all, and instructs the user to look at messages_dict. [1]

If constructed with a dictionary of error messages, then the method update_error_dict(self, error_dict) will happily accept error_dict=None - it checks for and handles this condition. If constructed with a string or list, then not supplying error_dict is an error.

This sort of inconsistent behaviour is hard to explain. At the least, this behaviour should be documented clearly, but ideally the interface should be consistent, regardless of how it is constructed.

[1] http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.clean

Change History (5)

comment:1 Changed 4 years ago by lukeplant

  • Severity set to Normal

comment:2 Changed 4 years ago by julien

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed
  • Type set to Cleanup/optimization

Just pointing out that cleaning up the interface is likely to break backwards compatibility, hence marking as DDN. I suggest that you write a patch with tests, so that we can see the extent to which this would break existing code.

comment:3 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:4 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:5 Changed 2 years ago by aaugustin

  • Resolution set to needsinfo
  • Status changed from new to closed

This ticket describes a valid problem, but it doesn't suggest any concrete steps for improvement. Marking as 'needsinfo' because it isn't actionable.

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