9578

OperationalError (1054, “Unknown column 'month' in 'field list'”)

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>

Answer1:

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

Recommend

  • More dynamic page with sitecake?
  • Complexity Of Dijkstra's algorithm
  • use preg_replace to replace whole words using associative array
  • Get rid of ^@ in vim output from system(…)
  • Add onClickListener to listview item
  • How can I use COALESCE() in WHERE clause optimally?
  • Why doesn't this jquery code work?
  • How to get a checkbox value using custom adapter and listview
  • Using sed with the EDITOR variable in a script for virsh or sudoers
  • Link_to remote => true causing strange routing problems, Rails 3
  • pywintypes.com error when running pyad.adgroup on flask
  • Attaching to processes in C
  • Returning multiple rows from a single row
  • Getting The Most Recent Data Item - Google App Engine - Python
  • set onClickListener in getview() with ViewHolder class
  • Vim key mapping works in command editor, not in .vimrc - why?
  • How to get an extra column in relational model in Django-rest-framework serializer?
  • Using PATH AMPL solver with Pyomo on Intel Mac OS X
  • Django how to modify database records by template
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • C Program Compiled with Cygwin and Called from Python Hangs
  • php is_file always return false
  • Django return user model id with L
  • Django model for a Postgres view
  • Trying to string.Join an IList
  • Combining two different ActiveRecord collections into one
  • Using Sax parsing to edit and write XML in VB6
  • Django simple Captcha “No module named fields” error
  • Remove final comma from string in vb.net
  • Django: Count of Group Elements
  • How to do unit test for HttpContext.Current.Server.MapPath
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • How to add date and time under each post in guestbook in google app engine
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Python: how to group similar lists together in a list of lists?
  • KeystoneJS: Relationships in Admin UI not updating
  • Busy indicator not showing up in wpf window [duplicate]