所以说我们有两个模型
class Product(models.Model):
""" A model representing a product in a website. Has new datapoints referencing this as a foreign key daily """
name = models.CharField(null=False, max_length=1024, default="To be Scraped")
url = models.URLField(null=False, blank=False, max_length=10000)
class DataPoint(models.Model):
""" A model representing a datapoint in a Product's timeline. A new one is created for every product daily """
product = models.ForeignKey(Product, null=False)
price = models.FloatField(null=False, default=0.0)
inventory_left = models.BigIntegerField(null=False, default=0)
inventory_sold = models.BigIntegerField(null=False, default=0)
date_created = models.DateField(auto_now_add=True)
def __unicode__(self):
return "%s - %s" % (self.product.name, self.inventory_sold)
目标是根据产品附带的最新数据点的stock_sold值对产品的QuerySet进行排序。这是我到目前为止的内容:
products = Product.objects.all()
datapoints = DataPoint.objects.filter(product__in=products)
datapoints = list(datapoints.values("product__id", "inventory_sold", "date_created"))
products_d = {}
# Loop over the datapoints values array
for i in datapoints:
# If a datapoint for the product doesn't exist in the products_d, add the datapoint
if str(i["product__id"]) not in products_d.keys():
products_d[str(i["product__id"])] = {"inventory_sold": i["inventory_sold"], "date_created": i["date_created"]}
# Otherwise, if the current datapoint was created after the existing datapoint, overwrite the datapoint in products_d
else:
if products_d[str(i["product__id"])]["date_created"] < i["date_created"]:
products_d[str(i["product__id"])] = {"inventory_sold": i["inventory_sold"], "date_created": i["date_created"]}
# Sort the products queryset based on the value of inventory_sold in the products_d dictionary
products = sorted(products, key=lambda x: products_d.get(str(x.id), {}).get("inventory_sold", 0), reverse=True)
这行得通,但是对于大量(500,000〜)产品和数据点来说,它却非常慢。有更好的方法吗?
还有一点(不重要),由于我对此一无所获,因此DataPoint模型的unicode方法似乎也在进行不必要的SQL查询。这是Django模型传递给模板后的默认特征吗?
答案 0 :(得分:2)
我认为您可以在此处使用subquery来注释最新数据点的值,然后对其进行排序。
基于这些文档中的示例,它将类似于:
from django.db.models import OuterRef, Subquery
newest = DataPoint.objects.filter(product=OuterRef('pk')).order_by('-date_created')
products = Product.objects.annotate(
newest_inventory_sold=Subquery(newest.values('inventory_sold')[:1])
).order_by('newest_inventory_sold')
从侧面来看,为避免在输出DataPoints时出现多余的查询,您将需要在原始查询中使用select_related
:
datapoints = DatePoint.objects.filter(...).select_related('product')
这将执行JOIN操作,以便获取产品名称不会引起新的数据库查找。