Django 中的自定义查询

发布于 2024-09-07 18:51:19 字数 887 浏览 5 评论 0原文

我正在建立一个电子商务网站。 我有一个产品模型,其中包含所有产品类型的通用信息:

class Product(models.Model):
  name=models.CharField()
  description=models.CharField()
  categories = models.ManyToManyField(Category)

然后我有 SimpleProduct 和 BundleProduct,它们具有产品的 FK 并保存特定于产品类型的信息。 BundleProduct 具有与其他产品的 m2m 字段。

class SimpleProduct(Product):
  some_field=models.CharField()

class BundleProduct(Product):
  products = models.ManyToManyField(Product)

显示目录时,我正在对产品模型进行一次查询 然后对每个产品进行另一个查询以获取附加信息。 这涉及到大量的查询。

我可以通过在 simpleproduct 和 bundleproduct 字段上使用 select_lated 来改进它。 我可以通过使用 select_reverse 应用程序来处理 m2m 字段(例如类别)来进一步改进它。

这是一个很大的改进,但需要更多查询,因为 BundleProduct 有多个产品,这些产品也可以与其他产品(可配置产品)相关。

有没有办法对 Product 进行单个查询来检索 m2m 类别、one2one SimpleProduct 和 BundleProduct 以及 BundleProduct 的产品?

这个自定义查询看起来像一个包含所有管理器和属性的 django 查询集吗?

谢谢

I'm building an ecommerce website.
I have a Product model that holds info common to all product types:

class Product(models.Model):
  name=models.CharField()
  description=models.CharField()
  categories = models.ManyToManyField(Category)

Then I have SimpleProduct and BundleProduct that have FK to Product and hold info specific to the product type. BundleProduct has a m2m field to other Products.

class SimpleProduct(Product):
  some_field=models.CharField()

class BundleProduct(Product):
  products = models.ManyToManyField(Product)

When displaying the catalog I'm making one query against the Product model
and then another query per product to get the additional info.
This involve a large number of queries.

I can improve it by using select_related on the simpleproduct and bundleproduct fields.
I can further improve it by using the select_reverse app for m2m fields like categories.

This is a big improvement but there are more required queries because a BundleProduct have several products which can also have relations to other products (configurable product).

Is there a way to have a single query against Product that will retrieve the m2m categories, one2one SimpleProduct and BundleProduct and the BundleProduct's products?

Will this custom query look like a django queryset with all the managers and properties?

Thanks

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

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

发布评论

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

评论(1

套路撩心 2024-09-14 18:51:19

您可以查看 查询集的额外方法。可能会让您有机会添加一些额外的字段。但如果您想要原始查询,可以使用 原始方法对于管理器来说,它们将返回一种查询集,但是它不会利用正常查询集的全部功能,但应该足以满足您的担忧。在同一页面上还显示了执行方法,这是真正的自定义 SQL,甚至无法转换为原始查询集。

You can possibly take a look at the extra method of querysets. May give you the opportunity to add some additional fields. But if you want raw queries, you can use the raw method of managers, these will return a type of queryset, that will not however harness the full power of normal querysets but should be enough for your concerns. On that same page the execute method is also shown, this is for truly custom sql that can't even translate into raw querysets.

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