Opened 6 years ago
Closed 6 years ago
#29806 closed New feature (duplicate)
Add parent_link to ForeignKey
Reported by: | Victor Porton | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I propose to allow parent_link
argument to ForeignKey
.
This would mean that a model is derived from another model, but instead of OneToOneField
we use ForeignKey
what means that several instances of the derived model may share the same instance of the parent model.
Let
class A(models.Model): x = models.IntegerField() class B(A): parent = ForeignKey(A, parent_link=True) y = models.IntegerField()
The rationale is the same as for existing model inheritance: I want to shorten code: Instead of b.parent.x
write just b.x
.
I see no other reason whatsoever (except that it a little complicates the understanding as every new feature does) to deny my feature. If it is done for OneToOneField
why not to do the same for ForeignKey
? There are no differences of arguments pro an contra for ForeignKey
and for OneToOneField
. Thus if this works for OneToOneField
it should also be done for ForeignKey
.
Also note that this new feature is useful for DB normalization: When the models structure changes, separating a parent object into a separate model, this feature would allow not to add .parent.
every time x
is used.
Duplicate of #29691 (closed as wontfix). As described there, it would likely add much complexity.