Python
Django

[django]ManyToManyField(中間モデル)の要素をクエリセットでフィルタリングする方法

models.py
class Person(models.Model):
    name = models.CharField(max_length=128)

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

class Membership(models.Model):
    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)
views.py
class GroupListView(ListView):

   def get_queryset(self):
        qs = super(GroupListView, self).get_queryset()
        return qs.filter(
          members__isnull=False,
          membership__date_joined__year='2018',
          membership__date_joined__month='01',
          membership__date_joined__day='17').distinct()