我想从我的项目中排除非活跃用户。
example 1:
url:users/1/friends/ will show all friends of that user.
我想只显示朋友列表中的活跃用户。
example 2:
url:users/1/friends/ will show all friends of that user.
如果 id 1 的用户不活跃,我不想显示他的任何活动..比如他的朋友..他的个人资料...等
example 3:
url:users/1/challenge/ will show all friends of that user in post form.
我不想以表单形式显示非活跃用户。
有没有通用的方法来执行此操作。因为这是一个大工程,我不可能到处都做过滤器。
表格如下:
class User(models.Model):
......
......
friends = models.ForeignKey(User)
您应该使用自定义模型管理器:
class ActiveUsersManager(models.Manager):
use_for_related_fields = True
def get_queryset(self):
return super(ActiveUserManager, self).get_queryset().filter(is_active=True)
class User(models.Model):
is_active = models.BooleanField(default=true)
# first manager is the default and accessible through objects.
active = ActiveUsersManager()
all_users = models.Manager()
active_users = User.objects.all()
all_users = User.all_users.all()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)