Opened 18 years ago
Closed 18 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: | dev |
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 |
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.
A
ForeignKey
withunique=True
is the way to go here, and so long as you don't putblank=True
on it you'll get the expected validation. The Django book, for example, recommends this for usingAUTH_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?