Friday, December 30, 2016

Filtering a many2many relationship while aggregating?

Hi,

I have the following many2many relationship between 2 models:

class RaceDay(models.Model):
    date
= models.DateField()
    published
= models.BooleanField(default=False)

class Series(models.Model):
    published
= models.BooleanField(default=False)
    year
= models.PositiveSmallIntegerField(default=2015)
    racedays
= models.ManyToManyField('RaceDay')




I can use the following annotation to get the Series ordered by the latest RaceDay in each series.

series_ordered_by_latest_raceday = Series.objects \
   
.annotate(latest_raceday=Max('racedays__date')) \
   
.filter(year=this_year, published=True) \
   
.order_by('latest_raceday')




The filter here applies to the fields in the Series model.

How would I limit the aggregation to members of the many2many relation that have RaceDay.published=True?

Thanks,
Richard Brockie

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c55c78b9-9774-4ba7-b858-b7af4e1aa506%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment