Can your seats be part of multiple trains? Is there a reason you defined a ManyToMany from Train to Seat instead of a ForeignKey from Seat to Train?
If your schema was defined as the latter the following could probably work (given you only want the first Seat of each Train object): trains = Train.objects.prefetch_related(first_seats=Seat.objects.order_by('train', 'order').distinct('train')) for train in trains: first_seat = train.first_seats[0] Else given you've explicitly defined a TrainSeat model to handle the through relationship to store the order: class TrainSeat(models.Model): train = models.ForeignKey(Train, related_name='train_seats') seat = models.ForeignKey(Seat) order = models.PositiveIntegerField() trains = Train.objects.prefetch_related(first_train_seats=Prefetch('train_seats', TrainSeat.objects.select_related('seat').order_by('train', 'order').distinct('train')) for train in trains: first_seat = train.first_train_seats[0].seat Hope it helps. Simon Le lundi 2 mars 2015 17:23:29 UTC-5, cool-RR a écrit : > > Hi, > > Say that I have a model Train and it has a ManyToMany to model Seat. I'm > looking at a queryset of Train and I want to do a prefetch_related on it, > but I don't want to get all the Seat objects; I want only the first Seat > object for each Train object. Is this possible with prefetch_related > and/or Prefetch? How? > > To clarify: My goal here is to save on querysets while still being able to > retrieve the first Seat object in each `train.seats`. > > > Thanks, > Ram. > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b3fcf574-9024-46f9-9499-c6e67d2e3a4b%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.