我有这些模型,我需要做一些计算并将其呈现给用户.我渲染了大约2-3k的行,这导致对数据库进行了4k个查询(如调试工具栏所示).有什么办法可以优化这个?我已经尝试过prefetch_related,但是它只是在已经完成的4k之上添加了另一个查询.
class Cart(models.Model):
name = models.CharField(max_length=15)
user = models.OneToOneField(User)
def sum_for_this(self, taxtype, tax):
return self.carttax_set.filter(tax__type__name=taxtype,
tax__tax=tax).aggregate(
sum=Coalesce(Sum('tax_amount'), Value('0'))
).get('sum')
class TaxType(models.Model):
name = models.CharField(max_length=10)
class Tax(models.Model):
name = models.CharField(max_length=100)
type = models.ForeignKey(TaxType)
tax = models.DecimalField()
class CartTax(models.Model):
cart = models.ForeignKey(Cart)
tax = models.ForeignKey(Tax)
base = models.IntegerField()
tax_amount = models.IntegerField()
我在模板中所做的是:
{% for cart in cartlist %}
{{ cart.sum_for_this }}
{% endfor %}
我已经尝试过但没有效果:
Cart.objects.prefetch_related('carttax_set').all()
该方法def sum_for_this正在执行所有查询.
解决方法:
from django.db.models import Q, F, Sum, Case, When, IntegerField
from django.db.models.functions import Coalesce
cartlist = Cart.objects.annotate(
sum=Coalesce(Sum(Case(
When(Q(carttax__tax__type__name=taxtype) & Q(carttax__tax__tax=tax), then=F('carttax__tax_amount')),
output_field=IntegerField()
)), 0)
)
并在模板中:
{% for cart in cartlist %}
{{ cart.sum }}
{% endfor %}
标签:postgresql,python,django
来源: https://codeday.me/bug/20191111/2018359.html