2

我有这两个模型;

class Point(models.Model):
    point       = models.IntegerField()
    description = models.CharField("Action", max_length=128)

class PointLog(models.Model):
    user   = models.ForeignKey('users.User', verbose_name ="Related User")
    point  = models.ForeignKey(Point)
    date   = models.DateTimeField(default = datetime.datetime.now())

Point对象是静态的。我的意思是它们不会动态变化,它显示了哪个动作带来了多少分。PointLog 对象显示用户的操作。一个用户可以完成多个相同的操作。

我想PointLog根据用户操作对对象进行分组并总结每个不同操作的点

帮助 :)

采样点对象;

description      point
-----------------------
make comment     5
add photo        20
add video        50

示例 PointLog 对象;

User        point         date
------------------------------
user1    make comment     ....
user1    make comment     ....
user1    add photo        ....
user1    add photo        ....
user1    add photo        ....
user1    add photo        ....
user1    add video        ....
user1    add video        ....
user1    add video        ....

样品结果;

user1s action;
  make comment 10 points gained
  add photo    80 points gained
  add video    150 points gained
4

2 回答 2

1

PointLog.objects.all().values('user','point__description').annotate(points_gained=Sum('point__point'))

于 2012-08-23T12:22:56.170 回答
0

用这个:

result = PointLog.objects.all().values("user", 'point').annotate(num=Count("pk"))
于 2012-08-23T12:19:09.017 回答