Re: [Django] #35965: GenericForeignKeys lose the assigned unsaved object after the object is saved (was: Let GenericForeignKeys behave like a ForeignKey w.r.t. unsaved objects)

3 views
Skip to first unread message

Django

unread,
Dec 3, 2024, 4:48:00 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
--------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* component: Database layer (models, ORM) => contrib.contenttypes
* stage: Unreviewed => Accepted
* summary: Let GenericForeignKeys behave like a ForeignKey w.r.t. unsaved
objects =>
GenericForeignKeys lose the assigned unsaved object after the object
is saved

Comment:

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):
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35965#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages