1

我计划在我正在创建的 Django 项目上创建各种排行榜。我希望排行榜显示当月的前 5 名用户。一个排行榜将显示推荐次数最多的前 5 位用户(Example_User 模型),另一个将显示从优惠中获得最多积分的用户(Lead 模型)。

class Example_User(AbstractUser):
    ...
    referred_by = models.ForeignKey('self', blank=False, null=True)

class Leads(models.Model):
    example_user = models.ForeignKey('Example_user')
    offer_name = models.CharField(max_length=50, blank=False, null=False, verbose_name="offer name")
    points = models.OneToOneField('Points', blank=False, null=False)
    lead_date = models.DateTimeField(auto_now_add=True, blank=False, verbose_name="the date the lead was completed")

class Points(models.Model):
    example_user = models.ForeignKey('Example_User')
    points = models.DecimalField(max_digits=15, decimal_places=2, blank=False, null=False, default=0, verbose_name="points")
    event_date = models.DateTimeField(auto_now_add=True, blank=False, null=False, verbose_name="date the points were earned")
    cash_conv = models.BooleanField(blank=False, null=False, default=False, verbose_name="money converted to points")
    from_offer = models.BooleanField(blank=False, null=False, default=True, verbose_name="points earned from offer")
    from_reward = models.BooleanField(blank=False, null=False, default=False, verbose_name="points spent on reward")

所以我有点卡住了。对于 Leads 模型,我最初想做类似以下的事情(见下文),但我不知道我会为 Sum 值添加什么。将整个 points 字段传递给 Sum 很麻烦,因为我只想传递 Points.points。

Leads.objects.filter(lead_date__month=DateTime.date.month()).annotate(total_points=Sum('points')

对于 Example_User 模型,我遇到了麻烦,因为它是一个自引用字段。我想返回推荐次数最多的前 5 名用户。不过,我在将其转换为代码时遇到了麻烦。谢谢!

4

0 回答 0