Opened 10 years ago
Last modified 10 years ago
#24495 closed Cleanup/optimization
Allow unsaved objects in ForeignKey check to be bypassed — at Initial Version
Reported by: | Karl Hobley | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8beta2 |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi everyone,
https://code.djangoproject.com/ticket/10811 implements a new check to prevent unsaved objects being set on a ForeignKey field.
A project I'm upgrading to Django 1.8 (Wagtail CMS/django-modelcluster) relies on this behaviour (for generating previews of pages without having to save them to the database. Pages can have other unsaved objects such as links or images linked to them).
See: https://github.com/torchbox/django-modelcluster#django-modelcluster
Unfortunately, this isn't an easy problem to solve without a small change to Django....
Proposed solution
Allow a flag to be specified on a subclass of ForeignKey which allows this check to be bypassed.
PR: https://github.com/django/django/pull/4334
django-modelcluster uses a subclass of ForeignKey called "ParentalKey". For example, in Wagtail all of the "child models" (eg, page links, images, etc) are linked to their Page model using a ParentalKey.
Setting this flag on the ParentalKey class fixes this issue: https://github.com/kaedroho/django-modelcluster/commit/915dc7e1cfc2462c2068ab9e9a334a7197935c7d