1

I am trying to define a custom QuerySet subclass, and attach it to my model using django-model-utils. In previous Django versions (I am using 1.9), PassThroughManager was used to accomplish this by the following code:

from model_utils.managers import PassThroughManager

class FooQuerySet(models.query.QuerySet):
    def my_custom_query(self):
        return self.filter(...)

class Foo(models.Model):
    # fields go here..

    objects = PassThroughManager.for_queryset_class(FooQuerySet)

As mentioned, it turns out that

PassThroughManager was removed in django-model-utils 2.4. Use Django’s built-in QuerySet.as_manager() and/or Manager.from_queryset() utilities instead.

I've tried to rewrite the code (sorry, if it looks too stupid, I have a couple of months of experience still doing some thinks blindly to meet deadlines)

class FooQuerySet(models.query.QuerySet):
    def my_custom_query(self):
        return self.filter(...)

class Foo(models.Model):
    # fields go here...
    objects = QuerySet.as_manager(FooQuerySet)

As for now, I ended up with TypeError: as_manager() takes exactly 1 argument (2 given). Could anyone please shed light in the correct syntax ?

Edgar Navasardyan
  • 4,261
  • 8
  • 58
  • 121

1 Answers1

1

You should call as_manager directly on FooQuerySet:

objects = FooQuerySet.as_manager()
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Daniel, the code works, but the strange think is that Eclipse puts red underlines (indicating seemingly invalid syntax) under key words like .filter, .all, and some of model names. And most surprisingly, the code compiles without any errors !!! Still this error signs in the editor are irritating. Can you comment on this ? – Edgar Navasardyan Aug 08 '16 at 15:53
  • 1
    Not really; static analysis rarely works well with dynamic languages like Python. Turn it off, if you can. – Daniel Roseman Aug 08 '16 at 15:56