Pra*_*tri 2 django python-2.7 django-rest-framework
我想从我的项目中排除非活动用户。
example 1:
url:users/1/friends/ will show all friends of that user.
Run Code Online (Sandbox Code Playgroud)
我只想在朋友列表中显示活跃用户。
example 2:
url:users/1/friends/ will show all friends of that user.
Run Code Online (Sandbox Code Playgroud)
如果ID为1的用户未处于活动状态,我不想显示他的任何活动..喜欢他的朋友..他的个人资料...等
example 3:
url:users/1/challenge/ will show all friends of that user in post form.
Run Code Online (Sandbox Code Playgroud)
我不想以表格形式显示非活跃用户。
有没有通用的方法可以执行此操作。因为这是一个很大的项目,我无法在所有地方都进行过滤。
表格如下:
class User(models.Model):
......
......
friends = models.ForeignKey(User)
Run Code Online (Sandbox Code Playgroud)
您应该使用自定义模型管理器:
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1836 次 |
| 最近记录: |