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

InheritanceManager queries always return an instance of InheritanceQuerySet #522

Open
ahmadkkhaled opened this issue Mar 22, 2022 · 2 comments

Comments

@ahmadkkhaled
Copy link

Problem

I am using a custom model manager that extends InheritanceManager like so:

class Manager(InheritanceManager):
   def custom_method(self):
         return self.filter(...)

because the return type of get_queryset() of InheritanceManagerMixin is hardcoded to return self._queryset_class(self.model) it's impossible for me to chain the methods of my custom Manager:

Model.objects.all().custom_method() will fail since .all() will return an instance of InheritanceQuerySet and my custom_method isn't defined there, it's rather defined on Manager.

Although this slight confusion is mentioned in the documentation here it's still not clear for the user how to properly extend InheritanceManager with custom managers

Environment

  • Django Model Utils version: 4.1.1
  • Django version: 3.2.11
  • Python version: 3.8

Code examples

Model.objects.custom_method() works
Model.objects.filter().custom_method() fails with exception 'InheritanceQuerySet' object has no attribute 'custom_method'

@ahmadkkhaled
Copy link
Author

related #205

@ahmadkkhaled
Copy link
Author

I managed to workaround this by extending InheritanceQuerySet like so:

CustomQuerySet(InheritanceQuerySet):
   def custom_function(self):
       ...

then using my CustomQuerySet in the model like so:

Model(models.Model):
   objects = CustomQuerySet.as_manager()

This way I retain djano-model-util InheritanceManager functionality and allow for chaining custom methods defined on the queryset, I still think this should be made more clear for the user in the documentation, because directly inheriting from InheritanceManager seems to cause problems

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

1 participant