Django ORM queryset将整数字段作为字符串

时间:2018-06-02 19:17:41

标签: python django django-queryset django-orm django-annotate

results = UmUrl.objects \
            .filter(created_at__gte=sdate, created_at__lte=edate) \
            .annotate(timeValue=self.get_date_format(param)) \
            .values('timeValue') \
            .order_by('timeValue') \
            .annotate(blocked=Sum(Case(When(operation=0, then=1), default=0, output_field=models.IntegerField()))) \
            .annotate(unBlocked=Sum(Case(When(operation=1, then=1), default=0, output_field=models.IntegerField())))

'operation'字段为Integer类型。此代码创建一个sql查询,但在sql reult'while'参数显示为字符串类型。所以它不起作用。

像这样的SQL输出。我该如何解决这个问题?

{"query": "SELECT DISTINCT CONCAT(EXTRACT('year' FROM \"um_url\".\"created_at\" AT TIME ZONE 'Europe/Istanbul'), CONCAT('-', EXTRACT('month' FROM \"um_url\".\"created_at\" AT TIME ZONE 'Europe/Istanbul'))) AS \"timeValue\", SUM(CASE WHEN \"um_url\".\"operation\" = '0' THEN '1' ELSE '0' END) AS \"blocked\", SUM(CASE WHEN \"um_url\".\"operation\" = '1' THEN '1' ELSE '0' END) AS \"unBlocked\" FROM \"um_url\" WHERE (\"um_url\".\"created_at\" <= '2018-06-02 15:47:51.857000+00:00' AND \"um_url\".\"created_at\" >= '2018-06-01 15:47:51.857000+00:00') GROUP BY CONCAT(EXTRACT('year' FROM \"um_url\".\"created_at\" AT TIME ZONE 'Europe/Istanbul'), CONCAT('-', EXTRACT('month' FROM \"um_url\".\"created_at\" AT TIME ZONE 'Europe/Istanbul'))) ORDER BY \"timeValue\" ASC", "empty_check": "true"}

0 个答案:

没有答案