Django PostgreSQL query not returning any values

Django version: 1.9.3 Python version: 3 PostgreSQL version: 9.4.6

Summary

I am trying to get the total count of "check_date" grouped by username. Both queries seem to be good, but Django does not return any result after running it. The same works as expected in PostgreSQL.

Details

Database Table:

 id |  username   | check_date 
----+-------------+------------
  1 | john        | 2016-03-26
  2 | john        | 2016-03-26
  3 | samantha    | 2016-03-26
  4 | samantha    | 2016-03-26
  5 | samantha    | 2016-03-26

Django Class:

class UserTable(models.Model):
 username = models.CharField(max_length=15)
 check_date = models.CharField(max_length=15, blank=True)

Django query:

date = str(u'''+date+''')
userCount = UserTable.objects.filter(check_date=date)
userCount = userCount.values('username').annotate(total = models.Count('username'))

Notice that since Django removes the quotes and thus affecting the query, I had to "tweak" the date variable (even the date field is not a DateField for now) before passing it to the filter.

Result of the Django Query ==> print (userCount.query)

SELECT "users_table"."username", COUNT("users_table"."username") AS "total" FROM "users_table" WHERE "users_table"."check_date" = '2016-03-26' GROUP BY "users_table"."username"

Running the query in PostgreSQL:

   username  | total 
-------------+-------
 john        |    2
 samantha    |    3

Running the query in Django:

[]

Any help is appreciated.

链接地址: http://www.djcxy.com/p/38696.html

上一篇: MySQL:什么是LIKE的反向版本?

下一篇: Django PostgreSQL查询不返回任何值