Reputation: 4352
I am sure this is not a novel/new problem. I really tried to look into other solutions. However, I could not find how to solve this.
I have a model like
class Deal(models.Model):
title = models.CharField(max_length=1500)
viewCounter = models.SmallIntegerField(default=0)
thumbsUpCounter = models.SmallIntegerField(default=0)
createDateTime = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=False, related_name='deal_owner',
editable=False)
Now I want to get top 10 (or less) deals ordering by thumbsUpCounter and viewCounter of every day. I tried to look into the Subquery and Outerref. However, I could not figure out how can I get the right results.
** I am using MySQL.
Thanks in advance.
Upvotes: 0
Views: 52
Reputation: 3248
try
from django.db.models.functions import TruncDate
query = Deal.objects.annotate(date=TruncDate('createDateTime'))\ # extract date
.values('date')\ # group by date
.order_by('-thumbsUpCounter')\ # order by
[:10] # slice first 10
Upvotes: 1