Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. month = InfraVltgDetail.objects.annotate(
  2. month=TruncMonth('create_dt'))
  3. .values('month').annotate(
  4. name_count=Count('id'))
  5. .values('month', 'name_count')
  6.  
  7. #print(by_month.query)
  8.  
  9. output:
  10.  
  11. SELECT django_datetime_trunc('month', "portal_infravltgdetail"."create_dt", 'UTC')
  12. AS "month", COUNT("portal_infravltgdetail"."id")
  13. AS "name_count" FROM "portal_infravltgdetail"
  14. GROUP BY django_datetime_trunc('month', "portal_infravltgdetail"."create_dt", 'UTC')
  15.  
  16. import psycopg2
  17.  
  18. conn = psycopg2.connect(database="test",
  19. user = "postgres",
  20. password = "postgres",
  21. host = "localhost",
  22. port = "5432")
  23. cur = conn.cursor()
  24. cur.execute(output_query)
  25. cur.fetchall()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement