python - 如何在 Django 中过滤属性

标签 python django

我目前在过滤数据时遇到问题。

型号

class Member(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    team_id = models.UUIDField(null=True)
    username = models.CharField('', max_length=30)
    is_active = models.BooleanField(default=False)

    @property
    def organization_id(self):
        """This is to get the organization Id
        """
        team = Team.objects.get(pk=self.team_id)
        return team.organization_id

现在我计划过滤所有 organation_id = 1 的成员。

这是我需要的:

memberList = Member.objects.filter(organization_id='1')

所以我得到了这个错误:

Cannot resolve keyword 'organization_id' into field. Choices are: id, is_active, team_id, username

如何使用 organization_id 过滤成员?

最佳答案

您的模型 Member 中没有任何名为 organization_id 的字段,这就是错误的原因。

相反你可能想要这个:

result_list = []
memberList = Member.objects.all()

for item in memberList :
    if item.organization_id() == '1' :
        result_list.append(item)

print result_list 

结果列表 result_list 将包含模型 Member 的所有必需对象。

谢谢。

关于python - 如何在 Django 中过滤属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41517441/

相关文章:

python - 使用 Python 3 的平均年龄计算器

python - 我是否需要使用 random.seed() 和 random.uniform() 来确保我得到不同的序列

python - Django Rest Framework 获取表单作为 json

python - Django 循环模型依赖

python - 如何从数据库中获取最近 24 小时的记录

javascript - 简单解释一下 BackboneJS 是如何工作的?

python - 在 Python 线程中导入

python - 我的脚本需要同时运行两个循环来读取来自两个不同来源的 IO 数据。我的线程正确吗?

python - pymc3 生成带有参数数组的随机变量

python - Django QuerySet 选择相关对象