在 django 中通过一个查询获取多行?

发布于 2024-11-15 01:33:08 字数 671 浏览 0 评论 0原文

如何构建一个从 django 获取多行的查询集?我认为 filter() 会起作用,但它似乎更糟。

例如,我在模型 Car 中有两行,具有两个文本属性(许可证和 vin)。现在假设我想打印这些汽车的执照和车辆证明。我怎样才能通过一次数据库调用来做到这一点?

这是一个将进行两次数据库调用的答案:

#using get(), two total queries
a = Car.objects.get(id=1) #query here
b = Car.objects.get(id=2) #query here
print(a.license + a.vin) #no query
print(b.license + b.vin) #no query

这显然不起作用,因为我进行了两次 get() 查询。接下来我将尝试 filter():

#using filter(), four total queries
c = Car.objects.filter(id__in=(1,2)) #no query
print(c[0].license + c[0].vin) #two queries
print(c[1].license + c[1].vin) #two queries

嗯,这很奇怪,为什么要进行四个数据库调用?有没有办法让它获得二合一数据库调用?

How can I get build a QuerySet that gets multiple rows from django? I thought filter() would work, but it seems to be worse off.

For example, I have two rows in the model Car, with two text attributes (license and vin). Now say I want to print the licenses and vins from these cars. How can I do that with one database call?

Here's an answer that will make two database calls:

#using get(), two total queries
a = Car.objects.get(id=1) #query here
b = Car.objects.get(id=2) #query here
print(a.license + a.vin) #no query
print(b.license + b.vin) #no query

That obviously didn't work because I made two get() queries. So next I'll try filter():

#using filter(), four total queries
c = Car.objects.filter(id__in=(1,2)) #no query
print(c[0].license + c[0].vin) #two queries
print(c[1].license + c[1].vin) #two queries

Hmmm, that's weird, why is making four database calls? Is there a way I can make it get the two in one database call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瑶 2024-11-22 01:33:08

由于查询集索引的工作方式,这看起来很奇怪。

c = list(Car.objects.filter(id__in=(1,2))) # query
print(c[0].license + c[0].vin) #no query
print(c[1].license + c[1].vin) #no query

如果您执行以下操作,您也只会有一个查询:

for car in Car.objects.filter(id__in=(1,2)):
    print(car.license + car.vin)

正如@Torsten 所说,在您的情况下,您似乎只是想获取您创建的所有汽车。这可以通过 all() 方法来实现:

for car in Car.objects.all():
    print(car.license + car.vin)

It's seems weird because of how indexing into a queryset works.

c = list(Car.objects.filter(id__in=(1,2))) # query
print(c[0].license + c[0].vin) #no query
print(c[1].license + c[1].vin) #no query

If you do the following, you'll only have one query too:

for car in Car.objects.filter(id__in=(1,2)):
    print(car.license + car.vin)

As @Torsten said, in your situation it appears like you're simply trying to get all the cars you've created. This can be achieved via the all() method:

for car in Car.objects.all():
    print(car.license + car.vin)
漆黑的白昼 2024-11-22 01:33:08

很好的例子。我认为在你的最后一个代码块中存在拼写错误。应该是:

for car in Car.objects.filter(id__in=(1,2)):
    print(car.license + car.vin)

该方法如何叠加

Great example. A typo I think though in your last codeblock. Should be:

for car in Car.objects.filter(id__in=(1,2)):
    print(car.license + car.vin)

How does that method stack up

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文