Django模型:用户过滤,总是 [英] Django model: Filtering by user, always

查看:83
本文介绍了Django模型:用户过滤,总是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何实现以下....

How do I achieve the following....

每当点对象显示在模板中时,必须始终由当前用户进行过滤。所以,在模型中,我尝试了下面的代码。

Every time the points object is displayed in a template it must always be filtered by the current user. So, within the model I tried the code below.

这是可能吗?如何实现上述?

Is this possible? how can I achieve the above?

Models.py

Models.py

from django.db import models
from django.contrib.auth.models import User


POINTS_PENDING, POINTS_ADDED, POINTS_DEDUCTED, ORDER_PROCESSING = range(4)
STATUS_OPTIONS = (
    (POINTS_PENDING, ('Pending')),
    (POINTS_ADDED, ('Added')),
    (POINTS_DEDUCTED, ('Deducted')),
    (ORDER_PROCESSING, ('Processing')),
    )


class PointsManager(models.Manager):

    def points_list(self,User):
        list = Points.objects.filter(points_user=User).exclude(status=ORDER_PROCESSING)
        return list


class Points (models.Model):
    user = models.ForeignKey(User)
    points = models.IntegerField(verbose_name=("Points"), default=0)
    created = models.DateTimeField(("Created at"), auto_now_add=True)
    updated = models.DateTimeField(verbose_name=("Updated at"), auto_now=True)

    objects = PointsManager()


推荐答案

您可以使用

@login_required 装饰器

然后您可以在您的视图中查询用户的点数

then you could could query for points by user in your view

user_points = Points.objects.filter(user = request.user)

或使用相反的 FK查找

request.user.points_set.all()

这篇关于Django模型:用户过滤,总是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆