Allow `GenericForeignKey` to retreive/store proxy model content type.
|Reported by:||charettes||Owned by:||charettes|
|Cc:||charette.s@…, danny.adair@…, anssi.kaariainen@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||yes|
The actual ContentTypeManager's get_for_model and get_for_models methods always return the ContentType of the concrete class of a proxy model. This behaviour is causing GenericForeignKey to save the concrete class of proxy model and creates inconsistencies in the ContentTypeManager's get_... API:
from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType class Worker(User): class Meta: app_label = 'app' proxy = True ct_get_for_model = ContentType.objects.get_for_model(Worker) ct_natural_key = ContentType.objects.get_by_natural_key('app', 'worker') assert ct_natural_key == ct_get_for_model # raises AssertionError
I don't know if it was a design decision (IHMO it's a mistake and a bug) to do so but changing it now would introduce a small backward compatibility issue for any app relying on get_for_model directly or indirectly through the use of `GenericForeignKey' since they would now benefit from getting the exact ct they were referencing.
If I'm not mistaken, by looking at regressiontests.defer_regress.tests.DeferRegressionTest and #10738, I assume this was introduced by r10523 to make sure deferred models are handled correctly by get_for_model. Checks for proxy reversing should be made against model._defered and not `model._meta.proxy'.
A discussion recently emerged of #11154 (which is caused by this behaviour) suggesting to either remove the proxy following routine of get_for_model(s?) or to allow passing a flag (follow_proxy) to opt-in for the correct behaviour.
I'm attaching a simple patch that only replaces the checks in _get_opts (and automatically fixes #11154) and I'll be marking the ticket as 'DDN' since it involves a small backward compatibility issue that requires feedbac.
Change History (25)
comment:1 Changed 5 years ago by charettes
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Type changed from New feature to Bug
comment:2 Changed 5 years ago by charettes
- Cc charette.s@… added
- Has patch set
- Triage Stage changed from Unreviewed to Design decision needed
comment:16 Changed 4 years ago by charettes
- Summary changed from Allow `ContentTypeManager` and `GenericForeignKey` to retreive/store proxy model content type. to Allow `GenericForeignKey` to retreive/store proxy model content type.
- Type changed from Bug to New feature
comment:17 Changed 3 years ago by jacob
- Triage Stage changed from Design decision needed to Accepted