[Django] #35965: Let GenericForeignKeys behave like a ForeignKey w.r.t. unsaved objects

2 views
Skip to first unread message

Django

unread,
Dec 2, 2024, 9:26:42 AM12/2/24
to django-...@googlegroups.com
#35965: Let GenericForeignKeys behave like a ForeignKey w.r.t. unsaved objects
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
As mentioned on [https://stackoverflow.com/q/79244238/67579
StackOverflow], a `ForeignKey` can still save an object if the object was
not saved when setting the model object, like:

{{{
one = Model_One()
two = Model_Two(model_one=one)
one.save()
two.save()
}}}

But this is not the case for a `GenericForeignKey` with:

{{{
one = Model_One()
two = Model_With_GFK(content_object=one)
one.save()
two.save()
}}}

This is because the `content_object` *immediately* sets the content type
field and the foreign key field when setting the object, and then leaves
is that way.

Perhaps overkill, but it might be worth to do the same for a `ForeignKey`
and thus check if the object has been given `.pk`, and if so, set the
foreign key field.
--
Ticket URL: <https://code.djangoproject.com/ticket/35965>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages