django-polymorphic icon indicating copy to clipboard operation
django-polymorphic copied to clipboard

Proxy Model Support - Creating base class and querying for proxy model does not return results

Open Eluzive opened this issue 6 years ago • 3 comments

Hello,

First off thanks for creating this package!

I am running into an issue now where we needed to create two different admin pages and creating proxy models seems like a good idea to use the same model have this separation or grouping. However, when registering the proxy models to their own separate ModelAdmins, the get_queryset() returns an empty set and results are not shown.

I've posted on SO

When I tried creating the object using the regular Django Model class, I am able to query the proxy model and get the created object.

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

class MyPerson(Person):
    class Meta:
        proxy = True

    def do_something(self):
        # ...
        pass
>>> p = Person.objects.create(first_name="foobar")
>>> p
<Person: Person object (4)>
>>> MyPerson.objects.all()
<QuerySet [<MyPerson: MyPerson object (4)>]>

However, when I use the PolymorphicModel and query for proxy model, the queryset is empty.


class Person(PolymorphicModel):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
>>> p = Person.objects.create(first_name="foobar")
>>> MyPerson.objects.all()
<PolymorphicQuerySet []>

Shouldn't we expect the Queryset to contain the Person object we created?

Eluzive avatar Jun 27 '19 18:06 Eluzive

We're also encountering this issue. :(

Any updates on this?

thejpanganiban avatar Jul 22 '20 10:07 thejpanganiban

Any updates??

ParikhKadam avatar Sep 12 '21 10:09 ParikhKadam

This is by design. For now you can work around this by overriding the manager's get_queryset:

class MyModelManager(PolymorphicManager):
    def get_queryset(self):
        qs = self.queryset_class(self.model, using=self._db, hints=self._hints)
        return qs


class MyModel(models.Usuario):
    objects = MyModelManager()

    class Meta:
        proxy = True

emyller avatar Apr 21 '22 13:04 emyller