0

在我看来,我得到了 points_total,但是它返回的所有点与 points_list 不同,它由当前用户过滤。

问题:

1) 我如何像在“points_list”中一样按用户过滤“def points_total”

模型.py

class PointsManager(models.Manager):

    def points_list(self,thisUser):
        list = Points.objects.filter(user=thisUser)
        return list

    def points_total(self,thisUser):
        return Points.objects.aggregate(models.Sum('points'))


class Points (models.Model):

    statusOptions = (
        (0, 'Pending'),
        (1, 'Added'),
        (2, 'Deducted'),
        (3, 'Processing'),
        )

    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)
    status = models.IntegerField(default=0, choices=statusOptions)

    objects = PointsManager()



    class Meta:
      verbose_name = ('Point')
      verbose_name_plural = ('Points')
4

1 回答 1

2

首先,我认为您的两个功能都不需要管理器,您可以通过查询自己的模型来直接实现。

point_list功能的替代品

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

points_total功能的替代品

points = Points.objects.filter(user=request.user) \
                       .aggregate(total_points=models.Sum('points'))
于 2012-12-25T12:07:06.417 回答