Django filter the model on ManyToMany count?

后端 未结 4 575
慢半拍i
慢半拍i 2020-12-13 23:30

Suppose I have something like this in my models.py:

class Hipster(models.Model):
  name = CharField(max_length=50)

class Party(models.Model):
  organiser =          


        
4条回答
  •  忘掉有多难
    2020-12-14 00:04

    Derived from @Yuji-'Tomita'-Tomita answer, I've also added .distinct('id') to exclude the duplitate records:

    Party.objects.filter(organizer=user, participants__isnull=False).distinct('id')
    

    Therefore, each party is listed only once.

提交回复
热议问题