Django:使用 F() 查询对象的行为不符合预期

发布于 2024-12-07 15:29:06 字数 370 浏览 0 评论 0原文

我试图导航到价格模型来比较价格,但遇到了意想不到的结果。

我的模型:

class ProfitableBooks(models.Model):
    price = models.ForeignKey('Price',primary_key=True)

在我看来:

foo = ProfitableBooks.objects.filter(price__buy__gte=F('price__sell'))

产生此错误:

'ProfitableBooks' object has no attribute 'sell' 

I am trying to navigate into the Price model to compare prices, but met with an unexpected result.

My model:

class ProfitableBooks(models.Model):
    price = models.ForeignKey('Price',primary_key=True)

In my view:

foo = ProfitableBooks.objects.filter(price__buy__gte=F('price__sell'))

Producing this error:

'ProfitableBooks' object has no attribute 'sell' 

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

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

发布评论

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

评论(1

埋葬我深情 2024-12-14 15:29:06

这是您的实际模型还是简化模型?我认为问题可能在于模型的唯一字段是主键,而外键是外键。如果我尝试解析它,它似乎暗示它本质上是一个充当查询集代理的字段——由于主键的性质,你永远不可能拥有比价格更有利可图的书籍。这似乎也意味着,由于隐含的唯一性限制,您的省略书籍字段的价格不得重叠。

如果我理解正确的话,您正在尝试比较另一个模型中的两个值:price.buy 与 Price.sell,并且您想知道这个未图示的 Book 模型是否有利可图。虽然我不确定 F() 对象在这里是如何分解的,但我的直觉是 F() 旨在促进一种有效的查询和更新,您可以根据其中的另一个值来比较或调整模型值。数据库。它可能无法处理像这样的“外壳”模型,该模型除了联合主/外键以及执行查询的模型外部的两个值的比较之外没有任何字段(并且也与书籍不同)我认为该模型具有有关书籍的识别信息)。

该文档说,只要您正在过滤而不是更新,您就可以在 F() 对象中使用联接,并且我假设您的价格模型具有买入和卖出字段,因此它似乎符合条件。所以我不能 100% 确定这在幕后发生了什么。但从实际角度来看,如果您想准确地完成此处隐含的结果,您只需对价格模型执行一个简单的查询即可,再次说明,ProfitableBooks 模型中没有明显的数据(它仅返回价格),并且您还暗示每个price.buy和price.sell都有一本对应的书。因此 Price.objects.filter(buy__gte=F('sell')) 给出了您在片段中请求的结果。

如果您想获取图书对象的结果,您应该执行像此处那样的查询,但从您的 Book 模型开始。如果您想以某种方式证实它,您可以将该查询放入名为“profitable_books”或其他名称的查询集管理器中。

Is this your actual model or a simplification? I think the problem may lie in having a model whose only field is its primary key is a foreign key. If I try to parse that out, it seems to imply that it's essentially a field acting as a proxy for a queryset-- you could never have more profitable books than prices because of the nature of primary keys. It also would seem to mean that your elided books field must have no overlap in prices due to the implied uniqueness constraints.

If I understand correctly, you're trying to compare two values in another model: price.buy vs. price.sell, and you want to know if this unpictured Book model is profitable or not. While I'm not sure exactly how the F() object breaks down here, my intuition is that F() is intended to facilitate a kind of efficient querying and updating where you're comparing or adjusting a model value based on another value in the database. It may not be equipped to deal with a 'shell' model like this which has no fields except a joint primary/foreign key and a comparison of two values both external to the model from which the query is conducted (and also distinct from the Book model which has the identifying info about books, I presume).

The documentation says you can use a join in an F() object as long as you are filtering and not updating, and I assume your price model has a buy and sell field, so it seems to qualify. So I'm not 100% sure where this breaks down behind the scenes. But from a practical perspective, if you want to accomplish exactly the result implied here, you could just do a simple query on your price model, b/c again, there's no distinct data in the ProfitableBooks model (it only returns prices), and you're also implying that each price.buy and price.sell have exactly one corresponding book. So Price.objects.filter(buy__gte=F('sell')) gives the result you've requested in your snipped.

If you want to get results which are book objects, you should do a query like the one you've got here, but start from your Book model instead. You could put that query in a queryset manager called "profitable_books" or something, if you wanted to substantiate it in some way.

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