Opened 3 hours ago

#35965 new Bug

Let GenericForeignKeys behave like a ForeignKey w.r.t. unsaved objects

Reported by: Willem Van Onsem Owned by:
Component: Database layer (models, ORM) Version: 5.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

As mentioned on 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.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top