Code

Opened 7 years ago

Closed 7 years ago

#3547 closed (duplicate)

OneToOneField is not validated as a required field, nor do explicitly defined isNotEmpty validators fire

Reported by: mrmachine <real dot human at mrmachine dot net> Owned by: adrian
Component: Database layer (models, ORM) Version: master
Severity: Keywords: OneToOne required validation validators
Cc: real.human@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

OneToOne field's don't properly validate the requirement that there _must_ be one object on each side of the relationship. after adding a model (Person) with a OneToOne field to User, i can go into the django admin and attempt to create a new person and leave the user field empty. no validation errors are returned (for example, "This is a required field"). if all other fields are valid, an uncaught type mismatch error will bubble up to the surface. i tried explicitly adding my own isNotEmpty validator to the field, but that doesn't seem to trigger, either.

is this just the way things are, and i should use ForeignKey with unique=True? this works, and i can still use u.get_profile() (if i've configured AUTH_PROFILE_MODULE in settings.py, but this doesn't help if i have multiple apps within a project which have their own distinct extensions to User. u.get_profile() will only work for one of them, and only works when extending User, not when extending any model in a OneToOne manner generally, and is also tied to the idea of the extending model being a "profile". imho it would be nice to keep the u.person mapping in the ORM, either by using OneToOne fields (which could just be a ForeignKey with unique=True behind the scenes, plus an extra mapping on the user instance), or by adding some logic to ForeignKey which adds the extra mapping if unique=True.

Attachments (0)

Change History (2)

comment:1 Changed 7 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

A ForeignKey with unique=True is the way to go here, and so long as you don't put blank=True on it you'll get the expected validation. The Django book, for example, recommends this for using AUTH_PROFILE_MODULE. The request extension to the ORM for a unique foreign key is probably a separate issue; would you mind opening a different ticket for it?

comment:2 Changed 7 years ago by Gary Wilson <gary.wilson@…>

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

dup of #1764.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.