1👍
✅
Yes, you can make a manager such that .objects
will only retain People
with graduated=False
:
class PeopleManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(graduated=False)
class People(models.Model):
name = models.CharField(max_length=20)
gender = models.CharField(max_length=20)
class = models.CharField(max_length=20)
graduated = models.BooleanField(default=False)
objects = PeopleManager()
all = models.Manager()
You can use People.all.all()
to retrieve all People
, and People.objects.all()
to retrieve all People
that did not graduate.
That being said, I do not recommend this: often People.objects.all()
gives the impression that one will retrieve all People
. As the Zen of Python says: "explicit over implicit": it is better that the code explains and hints what it is doing, not move filtering somewhere hidden in a manager.
Source:stackexchange.com