I want to exclude non-active users from my project.
example 1:
url:users/1/friends/ will show all friends of that user.
I want to show only a
If you want to exclude non-active users from a queryset you can use the following filter:
YourModel.objects.exclude(friend__is_active=False)
# or
YourModel.objects.filter(friend__is_active=True)
Where friend
is a ForeignKeyField
to a User
model object in YourModel
.
If you want a more general solution, you can use a ModelManager
:
class ActiveUsersOnlyManager(models.Manager):
def get_queryset(self):
return super(ActiveUsersOnlyManager, self).get_queryset().filter(is_active=True)
class User(models.Model):
is_active = models.BooleanField(default=true)
friends = models.ForeignKey(User)
# first manager is the default and accessible through objects.
objects = ActiveUsersManager()