InheritanceManager doesn't work with Proxy Model
See original GitHub issueI created an app to demonstrate this bug here https://github.com/chhantyal/mptt-test
Please see the models here https://github.com/chhantyal/mptt-test/blob/master/mptt_test/app/models.py
When I use select_subclasses(), it doesn’t give subclass object but superclass object (in this case ProxyPage object itself).
In [1]: from mptt_test.app.models import Blog, PageProxy
In [2]: Blog.objects.create(title='Title', quote='blog')
Out[2]: <Blog: Blog object>
In [3]: PageProxy.objects.all().select_subclasses()
Out[3]: [<PageProxy: PageProxy object>]
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:9
Top Results From Across the Web
Using InheritanceManager (django-model-utils) on proxy ...
I'm using InheritanceManager from django-model-utils to get the subclass of a given Django ... does it work when Specific is not a proxy?...
Read more >#24762 (Proxy models with children which inherit from the ...
I've not worked with proxies much, and certainly not recently, ... model as the parent for auto-downcasting model instances (using it's InheritanceManager )....
Read more >InheritanceManager - django-model-utils - Read the Docs
It allows queries on that base model to return heterogeneous results of the actual proper subtypes, without any additional queries.
Read more >[Solved]-Polymorphism in Django models-django
This is not what Proxy models are intended for but I wouldn't recommend using ... When polymorphism works well it's fine, but when...
Read more >QuerySets of various models
It also overcomes the third problem. We have distinct classes, which can have their own methods. We don't need to try to use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Same issue with:
Hi,
This hit me as well.
However, looking a bit more in detail into it, I feel like model-utils does the right thing / the best it can.
Proxy models in django only work on the Python level - there is no information stored about the proxy in the database (the table is shared with the base model). So when you query objects from a certain model (class), you get instances of that class. This is by design.
In that situation, model-utils has no way to know which model is “the right one” - because both are, there’s no difference on the db level! So it uses the non-proxy one in
select_subclasses.See also https://docs.djangoproject.com/en/3.1/topics/db/models/#querysets-still-return-the-model-that-was-requested.
(note: this is only my understanding of it, maybe I’m wrong)
EDIT:
To complete my point above, I would like to emphasize that proxy models in django are not subclasses in the MTI sense - they are other “views” of the same data. Accordingly, if you have a proxy model
Bto some modelA, you can create instances ofAorBand retrieve any of them later asAorBinstance transparently.In django, which model you want to retrieve instances of is explicit: from the model you take the manager of, from the definition of relation fields, from the arguments of
select_related, etc.In model-utils however the whole point of
select_subclassesis to “guess” the return types, by scanning through possible subclasses in the db. So proxy models cannot be retrieved as they do not correspond to tables in the db on their own (only present as a row indjango_content_type).One case where model-utils could do better (or forbid this case altogether) is when one explicitly passes a proxy model as argument in
select_subclasses:The first four calls to
select_subclassesmake sense. One could however argue that the last two should both succeed and returnEinstances.