Is is possible to set a default filter in Django models?
Something like:
class MyModel(models.Model):
timestamp = models.DateTimeField(default=datetime.utcnow)
active = models.BooleanField(default=True)
class Meta:
ordering = ['-timestamp']
filtering = [active=True]
You'll have to override the manager:
class MyModelManager(models.Manager):
def get_queryset(self):
return super(MyModelManager, self).get_queryset().filter(active=True)
class MyModel(models.Model):
timestamp = models.DateTimeField(default=datetime.utcnow)
active = models.BooleanField(default=True)
objects = MyModelManager()
class Meta:
ordering = ['-timestamp']
get_queryset
was get_query_set
before Django 1.6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With