Aha - I think this does it:

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




On Friday, December 30, 2016 at 4:49:51 PM UTC-8, Richard Brockie wrote:
>
> 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 [email protected].
To post to this group, send email to [email protected].
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/ebeb4982-2b4c-442a-8f7a-83604ea9b8a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to