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

问题描述 投票:1回答:1

我的代码设置如下:

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”(类别)验证和验证状态为“已批准”的用户列表。我怎样才能做到这一点?

django django-orm
1个回答
2
投票

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

 User.objects.filter(verifications__category="ID", verifications_status="APPROVED")
© www.soinside.com 2019 - 2024. All rights reserved.