I'm curious if there's a best practice, or recommended way to accomplish this?
Say I have a model like this:
class Cat(models.Model):
field1=models.CharField(...)
field2=models.CharField(...)
evil=models.BooleanField(...)
What I'm trying to accomplish is I want no views to ever be able to access Cat records where evil is True.
Do I really need to add .filter(evil=False) to every Cat.objects.filter call, or is there some way to do it once in the class and make the evil cats never show up anywhere?
Ok, a custom manager could fit in here. Just have a look into the docs. And like Chris Pratt said, keep in mind that the first manager becomes the default one.
Hope this leads into the right direction.
Update (maybe you could do it like this):
from django.db import models
class EvilCategoryManager(models.Manager):
def get_query_set(self):
return super(EvilCategoryManager, self).get_query_set().filter(evil=False)
class Cat(models.Model):
#.... atrributes here
objects = models.Manager()
no_evil_cats = EvilCategoryManager()
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