Opened 13 years ago

Closed 13 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 Holovaty
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: no UI/UX: no


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, 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.

Change History (2)

comment:1 Changed 13 years ago by James Bennett

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 13 years ago by Gary Wilson <gary.wilson@…>

Resolution: duplicate
Status: newclosed

dup of #1764.

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