问题描述
我从我的数据库中注明每个月的价格,我正在面对这个错误,我试图找出原因,所以在我的模型中,我有一个 DateTimeField
,我已经尝试使用shell中的数据,一切都很好,我有它,我也试图用MySQL Workbench查看数据库,数据在那里,和其余的代码,有人可以解释我的注释有什么问题吗? models.py
created = models.DateTimeField(auto_now_add = True)
views.py
from django.views.generic import ListView
from django.db.models import Count,Sum
from django.db import connection
from projects.models import Project
from .services import CurrencyConversionService
class ProjectStatisticsList(ListView):
model = Project
template_name ='sta tistics / statistics_list.html'
paginate_by = 10
def get_context_data(self,** kwargs):
context = super(ProjectStatisticsList,self).get_context_data(** kwargs)
truncate_month = connection.ops.date_trunc_sql(year,month)
total_price_per_month = Project.objects.extra({month:truncate_month})。 ).annotate(sum_price = Sum(price_aux))
上下文['total_price_per_month'] = total_price_per_month
返回上下文
在模板中:
< tr>
{%for total_price_per_month%}
< th class =aligh-left> < /第>
{%endfor%}
< td>月:{{data.month}}< / td>
< td>价格:{{data.sum_price}}< / td>
< / tr>
我不认为你可以使用 extra
在值
子句中。
但这是真的错误的做法。你应该使用Django的数据库功能支持;具体来说,:
从django.db.models.functions import提取
项目。 objects.annotate(month = Extract('created','month')。values(month)。annotate(sum_price = Sum(price_aux))
I'm annotating price per month from my database, and I'm facing this error, I'm trying to figure out why, so in my model I have a DateTimeField
, I have tried playing with data inside shell and everything is fine, I have it there, I also tried to look over db with MySQL Workbench and data is there, Full traceback and the rest of the code, can someone explain what is wrong with my annotation?
models.py
created = models.DateTimeField(auto_now_add=True)
views.py
from django.views.generic import ListView
from django.db.models import Count, Sum
from django.db import connection
from projects.models import Project
from .services import CurrencyConversionService
class ProjectStatisticsList(ListView):
model = Project
template_name = 'statistics/statistics_list.html'
paginate_by = 10
def get_context_data(self, **kwargs):
context = super(ProjectStatisticsList, self).get_context_data(**kwargs)
truncate_month = connection.ops.date_trunc_sql("year", "month")
total_price_per_month = Project.objects.extra({"month": truncate_month}).values("month").annotate(sum_price=Sum("price_aux"))
context['total_price_per_month'] = total_price_per_month
return context
And in template:
<tr>
{% for data in total_price_per_month %}
<th class="aligh-left"> </th>
{% endfor %}
<td>Month: {{ data.month }}</td>
<td>Price: {{ data.sum_price }}</td>
</tr>
I don't think you can use fields from extra
in a values
clause.
But this is really the wrong way to do it. You should be using Django's database function support; specifically, the extract
function:
from django.db.models.functions import Extract
Project.objects.annotate(month=Extract('created', 'month').values("month").annotate(sum_price=Sum("price_aux"))
这篇关于操作错误(1054,“字段列表”中的“未知列”月“”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!