Re: [Django] #35965: GenericForeignKeys lose the assigned unsaved object after the object is saved

18 views
Skip to first unread message

Django

unread,
Dec 3, 2024, 10:52:50 AM12/3/24
to django-...@googlegroups.com
#35965: GenericForeignKeys lose the assigned unsaved object after the object is
saved
--------------------------------------+------------------------------------
Reporter: Willem Van Onsem | Owner: (none)
Type: Bug | Status: new
Component: contrib.contenttypes | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Willem Van Onsem):

Probably we should also do a `.refresh_from_db()` since the
`GenericForeignKey` probably does some caching :).
--
Ticket URL: <https://code.djangoproject.com/ticket/35965#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 3, 2024, 4:19:51 PM12/3/24
to django-...@googlegroups.com
#35965: GenericForeignKeys lose the assigned unsaved object after the object is
saved
--------------------------------------+------------------------------------
Reporter: Willem Van Onsem | Owner: (none)
Type: Bug | Status: new
Component: contrib.contenttypes | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Brock Smickley):

Replying to [comment:1 Sarah Boyce]:
> Thank you for the report!
>
> Here is a potential test. The `tagged_item.save()` call currently raises
an error `django.db.utils.IntegrityError: NOT NULL constraint failed:
generic_relations_taggeditem.object_id`
> {{{#!diff
> diff --git a/tests/generic_relations/tests.py
b/tests/generic_relations/tests.py
> index e0c6fe2db7..32c90f2290 100644
> --- a/tests/generic_relations/tests.py
> +++ b/tests/generic_relations/tests.py
> @@ -626,6 +626,13 @@ class GenericRelationsTests(TestCase):
> with self.assertRaisesMessage(ValueError, msg):
> tagged_item.save()
>
> + def test_unsaved_generic_foreign_key_save(self):
> + quartz = Mineral(name="Quartz", hardness=7)
> + tagged_item = TaggedItem(tag="shiny", content_object=quartz)
> + quartz.save()
> + tagged_item.save()
> + self.assertEqual(tagged_item.content_object, quartz)
> +
> @skipUnlessDBFeature("has_bulk_insert")
> def test_unsaved_generic_foreign_key_parent_bulk_create(self):
> }}}
wait, do we actually want this test to pass? or do we want a similar test
with a `ForeignKey` to fail?
--
Ticket URL: <https://code.djangoproject.com/ticket/35965#comment:3>

Django

unread,
Dec 23, 2024, 10:33:52 AM12/23/24
to django-...@googlegroups.com
#35965: GenericForeignKeys lose the assigned unsaved object after the object is
saved
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner:
| YashRaj1506
Type: Bug | Status: assigned
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by YashRaj1506):

* owner: (none) => YashRaj1506
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/35965#comment:4>

Django

unread,
Dec 27, 2024, 5:34:52 PM12/27/24
to django-...@googlegroups.com
#35965: GenericForeignKeys lose the assigned unsaved object after the object is
saved
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner:
| YashRaj1506
Type: Bug | Status: assigned
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by YashRaj1506):

My question here is, having Foreignkey keep a unsaved object, is this a
behaviour we should keep? should this raise a error too when saved with
`save()` ? or we need to adapt the behaviour of `genericforeignkeys` so
that it can also keep an unsaved object? I just looked at this ticket, so
i lack a bit more deeper depth into this problem. I will do my research
but i am open to other peoples opinion on this.
--
Ticket URL: <https://code.djangoproject.com/ticket/35965#comment:5>

Django

unread,
Mar 25, 2025, 7:24:07 AM3/25/25
to django-...@googlegroups.com
#35965: GenericForeignKeys lose the assigned unsaved object after the object is
saved
-------------------------------------+-------------------------------------
Reporter: Willem Van Onsem | Owner:
| YashRaj1506
Type: Bug | Status: assigned
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Clifford Gama):

We allow model instances to reference unsaved objects, even though saving
them isn't permitted, because this behavior is essential for use cases
like inline formsets, where related objects can be linked to an instance
that is being created in another form.
--
Ticket URL: <https://code.djangoproject.com/ticket/35965#comment:6>
Reply all
Reply to author
Forward
0 new messages