#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701938beb51ee-99bcd5e6-a133-4e52-b886-88efbff3212f-000000%40eu-central-1.amazonses.com.

Reply via email to