Django根据额外条件一对多查询

时间:2019-03-19 04:08:18

标签: django django-orm

我的代码设置如下:

class User(models.Model):
   email = models.EmailField(_(u'email address'), max_length=255, unique=True, db_index=True, blank=True)

class Verification(models.Model):
   created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='verifications')
   category = models.CharField(choices=constants.VERIFICATION_CATEGORY_CHOICES, default=None, max_length=255)
   status = models.CharField(_(u'verification status'), max_length=255, default=constants.STATUS_PENDING, blank=True, null=True)

现在,我需要获取具有“ ID”(类别)验证并且验证状态为“已批准”的用户列表。我该如何实现?

1 个答案:

答案 0 :(得分:2)

您可以这样操作(使用related_name verifications作为反向关系):

 User.objects.filter(verifications__category="ID", verifications_status="APPROVED")