gpt4 book ai didi

python - 如何在 Django 中使用模型进行查询

转载 作者:行者123 更新时间:2023-11-28 20:31:38 25 4
gpt4 key购买 nike

假设我有一个表 usr,其中包含 feilds name 和 age表用户名

____________
id|name |age|

1 |dinkan | 12|

____________

我的问题是

select id from usr where name="dinkan" or age="20";

对应的输出应该是1

所以,我的问题是如何使用模型在 django 2.1 中实现它

最佳答案

您应该使用 django 模型提供的 Q 运算符。

Q objects provides you complete control over the where clause of the query.

使用这样的 orm 查询:

from django.db.models import Q

Table.objects.filter(Q(name="dinkan") | Q(age="20")).values("id")

您可以阅读更多相关信息 here

关于python - 如何在 Django 中使用模型进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54141934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com