加入现场的Django标注有额外 [英] Django annotation on field added with extra

查看:182
本文介绍了加入现场的Django标注有额外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方式来接管两个字段的注释上加在一起的典范。是这样的:

I'm trying to find a way to take an annotation over two fields on a model added together. Something like:

total_done = qs.values(
    'ability__ability_name',
).extra(
    select={
        'total_amount': 'effective_value + overage',
    }
).annotate(
    total=Sum('total_amount'),
).values(
    'ability__ability_name', 'total_amount'
).order_by('-total_amount')

以上不工作,并产生错误无法解析关键字TOTAL_AMOUNT成田
我已经尝试过在这里显示的解决方案:<一href=\"http://stackoverflow.com/questions/4567543/using-aggregate-on-a-value-introduced-using-extraselect-in-a-django-q/5195517#5195517\">Using .aggregate()上的数值推出了一款采用.extra(选择= {...})在Django查询?
但是没有运气仍然得到无法解析关键字TOTAL_AMOUNT成田

The above doesn't work and yields the error "Cannot resolve keyword 'total_amount' into field" I already tried the solution indicated here: Using .aggregate() on a value introduced using .extra(select={...}) in a Django Query? However no luck still get the "Cannot resolve keyword 'total_amount' into field"

不是执行的原始SQL查询作为的传递中会对它作出这样的前景有点复杂,已经进行了各种过滤器和排除查询字符串的任何其他。我也想避免增加现场实际的模型并计算它在保存,除非这是唯一的办法就是价值。

Anything other than performing the query in raw sql as the querystring that's being passed in could have various filters and excludes already performed on it making that prospect a little complicated. I'm also trying to avoid adding the field to the actual model and calculating it's value during save unless that's the only way.

推荐答案

尝试是这样的:

total_done = qs.extra(select = {'total_amount': 'SUM(one_column + another_column)'}, )

这篇关于加入现场的Django标注有额外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆