Django QuerySet 聚合和加入

ser*_*sk8 1 python django django-queryset

我对 Django 很陌生,我正试图弄清楚如何解决这个问题。这是我的模型:

class Product(models.Model):
    code = models.CharField(max_length=50)
    name = models.CharField(max_length=50)

class ProductDetail(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, editable=False)
    price = models.DecimalField(max_digits=5, decimal_places=2, default=Decimal('999.99'))
    available = models.BooleanField(default=True)
    validity_tms = models.DateTimeField(default=timezone.now)
Run Code Online (Sandbox Code Playgroud)

基本上,对于每个产品,我想跟踪价格和可用性的变化。因此,每个产品都存在许多 ProductDetail。

需要时,我需要获取每个产品的codenameavailableprice字段,但只与每个产品的最大validity_tms 的 ProductDetail 相关。

这个 QuerySet 只包含我需要的部分信息:

ProductDetail.objects.values('product').annotate(max_date=Max('validity_tms'))
Run Code Online (Sandbox Code Playgroud)

我也如何检索价格可用字段?

有没有办法解决这个问题?我在模型中做错了吗?

Tod*_*dor 5

@Todor 我需要最伟大的那个 validity_tms

那么你基本上是在寻找一个最大的 1-per-group查询。

但这不在ORM的能力范围内,可以通过单个查询来完成。

更新

这个问题让我做了一些挖掘,我刚刚发现我们实际上可以使用来自Django 1.11. 我们通过这样做子查询OuterRef,这里是如何。

latest_product_details = ProductDetail.objects.filter(
    validity_tms=Subquery(
        (ProductDetail.objects
            .filter(product=OuterRef('product'))
            .values('product')
            .annotate(max_date=Max('validity_tms'))
            .values('max_date')[:1]
        )
    )
)

#now you can choose to work with ProductDetail itself:
for product_detail in latest_product_details.select_related('product'):
    product = product_detail.product
    print (product, product_detail)


#or you can work with Product objects and prefetch those latest details
products = Product.objects.my_complex_filtering().prefetch_related(
    models.Prefetch('productdetail_set',
        queryset=latest_product_details,
        to_attr='latest_details'
    )
)
for product in products:
    #get the first element in the list or None if Empty
    last_detail = next(iter(product.latest_details), None)
    print (product, last_detail) 
Run Code Online (Sandbox Code Playgroud)

更新结束。

旧的答案仍然适用于之前的版本 Django 1.11

我将通过 2 个查询为您提供另一种方法。这种方法不适用于每组有很多对象没有机会将组过滤到只有几个元素的情况。这是因为,当您每组只需要 1 个时,所有对象都将被加载到内存中。

products = (Product.objects
    .annotate(max_date=Max('productdetail__validity_tms'))
    .prefetch_related(models.Prefetch('productdetail_set',
        #this will fetch all related ProductDetails for a product
        #if one product has many of them, consider further limiting
        #the queryset with some additional filter
        #e.g. only details from the last year or something like that.
        #the idea is to lower the memory footprint, since you need only the first one
        queryset=ProductDetail.objects.order_by('-validity_tms'),
        to_attr='details'
    ))
)

#usage
for product in products:
    print({
        'product': product.id,
        'max_date': product.max_date,
        'detail_price': product.details[0].price if product.details else None,
        'detail_available': product.details[0].available if product.details else None,
    })
Run Code Online (Sandbox Code Playgroud)