我如何实现以下目标......
每次点对象显示在模板中时,必须始终由当前用户进行过滤。所以,在模型中我尝试了下面的代码。
这可能吗?我怎样才能实现上述目标?
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()
答案 0 :(得分:13)
您可以使用
确保您的观看次数有用户然后您可以在视图中按用户查询点数
user_points = Points.objects.filter(user=request.user)
或使用反向FK lookup
request.user.points_set.all()