【发布时间】:2019-08-16 18:36:14
【问题描述】:
我是Django ORM 的新手,我发现在sql 语句中使用Django 子查询很困难,因为我没有在django orm 中找到嵌套的select .. from (select... 示例:
这些是我的模型:
class A:
published_at = models.DateTimeField(_('Published at'))
....
Class B:
pub=models.ForeignKey('A', verbose_name=_('A'), blank=True, null=True,
on_delete=models.SET_NULL)
prices= models.FloatField(_('Price'), blank=True, null=True, db_index=True)
soc = models.IntegerField(_('SOC'), blank=True, null=True,
db_index=True)
这是SQL
select DATE_FORMAT(`A`.`published_at`, '%Y-%m-%d'), sum(b)
from (
select `B`.`pub_id` as c, soc, avg(prices) as b
from B
group by c, soc
) as ch
INNER JOIN `A` ON (c = `A`.`id`)
group by DATE_FORMAT(`A`.`published_at`, '%Y-%m-%d');
在这种情况下使用“子查询”有用吗?我正在使用 django 1.11
请帮忙
更新
当我尝试Endre Both 建议的解决方案时,我得到了这个错误
Traceback (most recent call last):
File "/home/vagrant/.local/lib/python3.6/site-
packages/IPython/core/interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-7-824230af12bd>", line 5, in <module>
.annotate(total=Sum('avg'))
File "/home/vagrant/.local/lib/python3.6/site-packages/django/db/models/query.py", line 948, in annotate
clone.query.add_annotation(annotation, alias, is_summary=False)
File "/home/vagrant/.local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 973, in add_annotation
summarize=is_summary)
File "/home/vagrant/.local/lib/python3.6/site-packages/django/db/models/aggregates.py", line 19, in resolve_expression
c = super(Aggregate, self).resolve_expression(query, allow_joins, reuse, summarize)
File "/home/vagrant/.local/lib/python3.6/site-packages/django/db/models/expressions.py", line 548, in resolve_expression
c.source_expressions[pos] = arg.resolve_expression(query, allow_joins, reuse, summarize, for_save)
File "/home/vagrant/.local/lib/python3.6/site-packages/django/db/models/expressions.py", line 471, in resolve_expression
return query.resolve_ref(self.name, allow_joins, reuse, summarize)
File "/home/vagrant/.local/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1472, in resolve_ref
return self.annotation_select[name]
KeyError: 'avg'
【问题讨论】:
-
如果 SQL 语法有效(我不认为它是有效的,你运行了吗?),你似乎加入了两个主键。我不明白这有什么意义。
-
如果两个模型之间有关系,用Django relationships表示。
-
是的,有一个我忘记提及的relashionship..你能看看我的编辑吗
标签: django django-orm