escaping for objects.raw()

前端 未结 2 1926
-上瘾入骨i
-上瘾入骨i 2021-01-15 01:55

I need a custom SQL query and try to use this:

Log.objects.raw(\"SELECT pub_date FROM foo_log GROUP BY DATE_FORMAT(pub_date,\'%Y%m%d\')\")

相关标签:
2条回答
  • 2021-01-15 02:13

    Generally, you escape a % with another %, e.g.:

    Log.objects.raw("SELECT pub_date FROM foo_log GROUP BY DATE_FORMAT(pub_date,'%%Y%%m%%d')")
    
    0 讨论(0)
  • 2021-01-15 02:16

    Rather then raw SQL you could use dates for the above query.

    Log.objects.dates('pub_date', 'day')
    

    would return all the distinct dates by y/m/d

    0 讨论(0)
提交回复
热议问题