[Django]-Django global filter

0👍

What about creating a view in the database with the filter and create a Django model pointing to the view?

0👍

You should make a custom manager and modify an initial QuerySet. Check out the docs.

# First, define the Manager subclass.
class DahlBookManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(author='Roald Dahl')

# Then hook it into the Book model explicitly.
class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)

    objects = models.Manager() # The default manager.
    dahl_objects = DahlBookManager() # The Dahl-specific manager.

Then you should use your custom manager (dahl_objects) instead of objects and all queries will be modified.

Or you can override objects manager itself

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)

    objects = DahlBookManager() # The Dahl-specific manager
👤Danil

Leave a comment