Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Raw SQL give me TypeError not enough arguments

Tags:

sql

django

code:

 diary=models.SablogArticles.objects.raw("SELECT articleid,DATE_FORMAT(from_unixtime(dateline),'%Y-%m')\
 as newtime,count(*) as howmany   FROM sablog_articles group by newtime")

The result gave me :

In template d:\python\project\tpl\base.html, error at line 68 Caught TypeError while rendering: not enough arguments for format string

like image 920
Beyond Avatar asked Sep 15 '11 01:09

Beyond


1 Answers

The raw SQL is a string with formatting parameters, which means that % indicates a parameter to format. Your string has % in it. You need to double them to protect them from interpretation:

diary = models.SablogArticles.objects.raw("""
    SELECT 
        articleid, 
        DATE_FORMAT(from_unixtime(dateline),'%%Y-%%m') as newtime,
        count(*) as howmany
    FROM sablog_articles group by newtime
    """)
like image 123
Ned Batchelder Avatar answered Oct 15 '22 00:10

Ned Batchelder