Reputation: 864
I got two models :
models.py
class Representation(models.Model):
datetime = models.DateTimeField()
show = models.ForeignKey(Show)
class Show(models.Model):
name = models.CharField(max_length=512)
and then I'd like to have the next 5 incoming shows, and the best i can do is a clever hack like that, but this just uses a ton of queries, and I'm sure there is a more pythonic way to do it
i=5
list_shows = set()
while (len(list_shows) < 5:
for rep in Representation.objects.filter(datetime__gt=datetime.now()).order_by('-datetime')[:i]:
list_shows.add(rep.show)
i+=1
if i>100:
break
I also tryed something like this :
from datetime import datetime
from django.db.models import Min
Show.objects.annotate(date=Min('representation__datetime')).filter(date__gte=datetime.now()).order_by('date')[:5]
But this doesn't take the case where a show already played yesterday but play against tonight.
Upvotes: 0
Views: 330
Reputation: 16010
Can you try this:
Show.objects.filter(representation_set__datetime__gt=datetime.now()).order_by('representation_set__datetime')[:5]
Upvotes: 2