Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use two managers in one model #270

Closed
darbre opened this issue Jun 3, 2022 · 5 comments
Closed

Use two managers in one model #270

darbre opened this issue Jun 3, 2022 · 5 comments

Comments

@darbre
Copy link

darbre commented Jun 3, 2022

Hi!

I have a Place model with subclasses Restaurant and Bar. I attached InheritanceManager from django-model-utils to Place to use the select_subclasses() method to get instances of the subclass.

from model_utils.managers import InheritanceManager


class Place(models.Model):
    # ...
    objects = InheritanceManager()

class Restaurant(Place):
    # ...

class Bar(Place):
    # ...

Everything worked fine. But now I want to set the order of the model Place.

...
objects = OrderedModelManager()
...

How to combine them?
Thanks.

@imomaliev
Copy link
Collaborator

You need to create new Manager class that inherits from both InheritanceManager and OrderedModelManager

class PlaceManager(InheritanceManager, OrderedModelManager)
    ...
    
class Place(models.Model):
    # ...
    objects = PlaceManager()

@darbre
Copy link
Author

darbre commented Jun 4, 2022

I have tried this method. If I try to move the object in the admin, I got an error:

AttributeError at /admin/myapp/place/6/move-top/
'InheritanceQuerySet' object has no attribute 'filter_by_order_with_respect_to'

@imomaliev
Copy link
Collaborator

You need to overload to queryset as well. And this queryset should be subclass of OrderedModelQueryset

@darbre
Copy link
Author

darbre commented Jun 4, 2022

objects = InheritanceManager.from_queryset(OrderedModelQuerySet)()

error: AttributeError
'OrderedModelQuerySet' object has no attribute 'select_subclasses'

@imomaliev
Copy link
Collaborator

You should've created new QuerySet that inherits OrderedModelQuerySet and InheritanceQuerySet and then passed it to your new PlaceManager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants