我打算在我正在创建的Django项目上创建各种排行榜。我希望排行榜能够显示本月的前5位用户。一个排行榜将显示排名最高的前5位用户(Example_User模型),另一位将显示从优惠中获得最多积分的用户(领导模型)。
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值做什么。将整个点字段传递给Sum很麻烦,因为我只想传递Points.points。
Leads.objects.filter(lead_date__month=DateTime.date.month()).annotate(total_points=Sum('points')
对于Example_User模型,我遇到了麻烦,因为它是一个自引用字段。我想返回推荐人数最多的前5位用户。我在将其转换为代码时遇到了麻烦。谢谢!