【发布时间】:2018-10-23 18:56:25
【问题描述】:
我刚开始使用 ORM 来执行我的数据库调用,我很难找到优化我的代码/查询的好方法。
我有可以在 ProductGroups 中的产品。产品可以有 0 到 n 个价格。价格由其 ActorLocation 及其值定义。 我想显示 ProductGroup 中所有产品的最低和最高价格。
我编写的代码可以运行,但由于数据库访问量大,运行速度非常慢。任何有助于朝着好的方向发展的帮助将不胜感激:)
型号:
class ProductGroup(OwnedClass):
name = models.CharField(max_length=200, blank=False)
class Product(models.Model):
name = models.CharField(max_length=200, blank=True)
internal_code = models.CharField(max_length=200, blank=True)
description = models.CharField(max_length=1000, blank=True)
product_group = models.ForeignKey(
ProductGroup, blank=True, null=True, on_delete=models.CASCADE)
class Price(models.Model):
price_without_taxes = models.FloatField(default=None, null=True)
location = models.ForeignKey(
ActorLocation, blank=True, null=True, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
class ActorLocation(models.Model):
name = models.CharField(max_length=200, blank=False, null=False)
这是我生成发送到我的 ProductGroupSerializer 的上下文的函数:
def get_context(product_groups):
locations = company.actor_locations.all()
for product_group in product_groups:
price_list = []
products = product_group.product_set.all()
for product in products:
for location in locations:
price = Price.objects.filter(product=product, location=location).last()
if price:
price_list.append(price.price_without_taxes)
if price_list:
context = {'lowest_product_price': round(min(price_list), 2), 'highest_product_price': round(max(price_list), 2)}
else:
context = {'lowest_product_price': "", 'highest_product_price': ""}
contexts[product_group.id] = context
return contexts
【问题讨论】:
-
看看 select_related() 和 prefetch_related() docs.djangoproject.com/en/2.0/ref/models/querysets/… 。另见docs.djangoproject.com/fr/2.0/topics/db/optimization/…
标签: django database optimization django-rest-framework