13

我如何实现以下....

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

这可能吗?我怎样才能实现上述目标?

模型.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()
4

1 回答 1

14

您可以通过使用确保您的视图有用户

@login_required装饰师

那么您可以在您的视图中按用户查询积分

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

或使用反向FK 查找

request.user.points_set.all()

于 2012-12-25T00:33:38.223 回答