Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 操作错误(1054,“字段列表”中的“未知列”“月份”“月份”)_Python_Mysql_Django - Fatal编程技术网

Python 操作错误(1054,“字段列表”中的“未知列”“月份”“月份”)

Python 操作错误(1054,“字段列表”中的“未知列”“月份”“月份”),python,mysql,django,Python,Mysql,Django,我从我的数据库中注释每月的价格,我面临这个错误,我试图找出原因,所以在我的模型中我有一个DateTimeField,我尝试在shell中处理数据,一切都很好,我有它,我还尝试用MySQL Workbench查看数据库,数据在那里,其余的代码,有人能解释一下我的注释有什么问题吗 models.py created = models.DateTimeField(auto_now_add=True) views.py from django.views.generic import ListView

我从我的数据库中注释每月的价格,我面临这个错误,我试图找出原因,所以在我的模型中我有一个
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 = '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
在模板中:

<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>

{每月总价格%中的数据百分比}
{%endfor%}
月份:{{data.Month}
价格:{data.sum_Price}

我认为您不能在
子句中使用
extra
中的字段

但这确实是错误的做法。您应该使用Django的数据库功能支持;具体而言,委员会:


可能不是问题所在,但是
data.month
不在for循环的范围内,而
data
是循环变量。您到底想查询什么。这似乎不是ORM无法处理的事情。@schwobasegl我想查询我每月的项目总价格,并制作一个12个月的表格,以便显示每个月的总价格。啊,我使用的是
django
1.8.9版,这是在1.10中添加的
from django.db.models.functions import Extract
Project.objects.annotate(month=Extract('created', 'month').values("month").annotate(sum_price=Sum("price_aux"))