django - Django根据额外条件进行一对多查询
问题描述
我有这样的代码设置:
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”(类别)验证且验证状态为“已批准”的用户列表。我怎样才能做到这一点?
解决方案
你可以这样做(使用related_nameverifications
作为反向关系):
User.objects.filter(verifications__category="ID", verifications_status="APPROVED")
推荐阅读
- css - 在 Firefox 中对背景剪辑(ed)文本进行了两次转换
- java - 与 Hazelcast 共享 Hibernate 5 二级缓存
- c# - Alglib 卡方分布示例
- javascript - 为什么我确实有以下类型错误:无法读取未定义的属性“长度”?(在反应中)
- android - 记住 LazyColumn 滚动位置 - Jetpack Compose
- webrtc - 特定对等方的 STUN 绑定(ICE 连接检查)失败
- neovim - 如何更改 coc.nvim 中 pylint 的路径?
- javascript - 尝试在 React Native 中更新数组对象
- ssis - 如果进程今天已经运行,如何禁用任务
- php - 以下代码未达到 else 语句