Ron
Ron

Reputation: 23486

django - Way to avoid custom manager for specific queries

I have the following issue: I am working on a grown project which uses the pattern of overriding the get_queryset() method in the manager.

# Model declaration
class MyModel(models.Model):
    ...

    objects = MyModelManager()


# Manager declaration
class MyModelManager(models.Manager):

    def get_queryset(self):
        return super(MyModelManager, self).get_queryset().exclude(is_visible=False)

This causes some of the records to become basically invisible when you use the django ORM.

I need now in certain edge cases to use the base get_queryset() method and NOT to use the custom one.

I could clean up and change all the code but it would be a lot of work.

So my question: Is there a way to make a query like this MyModel.objects.all() and avoid using the custom manager method?

Hope I made my point clear enough.

Thanks,
Ron

Upvotes: 3

Views: 480

Answers (2)

Naveen Varshney
Naveen Varshney

Reputation: 21

You can use another custom manager which does not override get_queryset() method and it would be better if you use different name then common objects name that you have to define custom manager only once.

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

You can add multiple managers to your model. For example:

class MyModelManager(models.Manager):

    def get_queryset(self):
        return super(MyModelManager, self).get_queryset().exclude(is_visible=False)


class MyModel(models.Model):
    # …

    objects = MyModelManager()
    all_objects = models.Manager()

If you then need all the objects, you can thus access these with MyModel.all_objects.all().

Upvotes: 4

Related Questions