[Django]-Django – Way to avoid custom manager for specific queries

4👍

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().

0👍

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.

Leave a comment