Edgar Navasardyan
Edgar Navasardyan

Reputation: 4501

Attaching custom queryset to model via django-model-utils

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 ?

Upvotes: 1

Views: 168

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You should call as_manager directly on FooQuerySet:

objects = FooQuerySet.as_manager()

Upvotes: 1

Related Questions