In a Django QuerySet, how to filter for “not exists” in a many-to-one relationship

后端 未结 7 1840
失恋的感觉
失恋的感觉 2020-12-23 19:23

I have two models like this:

class User(models.Model):
    email = models.EmailField()

class Report(models.Model):
    user = models.ForeignKey(User)


        
相关标签:
7条回答
  • 2020-12-23 19:49

    To filter users who do not have reports associated with them try this:

    users = User.objects.exclude(id__in=[elem.user.id for elem in Report.objects.all()])

    0 讨论(0)
提交回复
热议问题