Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:01:40+00:00 2026-06-16T04:01:40+00:00

I have a Django application which generates some graphs with Google Visualization API (Google

  • 0

I have a Django application which generates some graphs with Google Visualization API (Google charts) based on django aggregate querysets. The Google Charts API expects the data to be in correct format to plot generate the graph correctly (ie. dates need to be of javascript type Date, not integer or float).

Say I want to create an aggregate for my model Posts to display for example total number of records by a particular user per year. My code would look something like this:

qset = Post.objects.filter(user__id=1).extra(select={"year": "date_part('year', cdt)"}\
          .values("year").annotate(num_records=Count("id")).order_by()

This work quite nice, except that the date_part() postgresql function which is used to extract the year from a timestampz field returns as a float, not an int, which in case mess up my chart data.

Running a similar query from psql returns a integer, which is what I expects.

select date_part('year', cdt) from public.core_post where id=6543;
  date_part 
  -----------
  2011
(1 row)

Again, running a small python program using the psycopg2 library gives me the same problem:

import psycopg2

def main():
    conn_string = "host='localhost' dbname='mydb' user='username' password='secret'"
    conn = psycopg2.connect(conn_string)
    cursor = conn.cursor()

    sql = "select date_part('year', cdt) from public.core_post where id=6543;"
    cursor.execute(sql)

    for rec in cursor.fetchone():
        print type(rec), rec

if __name__ == '__main__':
    main()

<type 'float'> 2011.0
[Finished in 0.1s]

Anybody got a clue why, or if it’s fixable?

(I can of course write a function which fix this before passing data to my graph library, but I’d rather be able to pass in the queryset (because that’s how I’ve written my graph lib).)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:01:41+00:00Added an answer on June 16, 2026 at 4:01 am

    Okay, embarrassing.
    Just re-read the PostgreSQL documentation about date_part which clearly says:

    Function                    Return Type
    date_part(text, interval)   double precision
    

    I’ll just accept this as an answer, and leave it for future reference.

    Solved by casting the result of date_part() to int.

    qset = Post.objects.filter(user__id=1).extra(
               select={"year": "date_part('year', cdt)::int"}) \
               .values("year").annotate(num_records=Count("id")).order_by()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django application which sometimes needs to send some data through TCP
I have a django application which is exposing a API whose url conf look
I have a Django application which I decided to port to Google App Engine.
I would like to deplay a django application/project, which i have created within Aptana.
I have some unit tests I've written to test my Django application. One test
I have a django application which has one main table/model which references various 'lookup'
I have a Django application which edits a database table, which another application polls
I have a view my Django application which when invoked calls my backend. My
I have a Django application which resets unix user passwords running in an Ubuntu
I have a a django application which is being served from apache/mod_wsgi under www.mysite.com/mysite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.