Django原始查询动态查询

时间:2016-02-24 11:58:49

标签: python mysql django

我有这个型号:

query_text = 'SELECT *'
for s in StockLocation.objects.all():
    query_text += ', SUM(CASE WHEN stocklocationid_id = ' + str(s.stocklocationid) + ' THEN quantity ELSE 0 END) AS ' + s.sl_name
query_text += ' FROM stock GROUP BY productid'
datatest = Stock.objects.raw(query_text)

要填写表格(django-tables2)并在第一列中使用stocklocation,我需要这样的查询。

score = raw_input('what is your score?')
try:
    score = float(score)
except:
    score = -1
    print 'Bad Score, Try Again'

但它不起作用。 Django能够管理吗?

1 个答案:

答案 0 :(得分:0)

表名的格式为appname_modelname。例如,如果您的应用名称为mystock,则您的SQL语句应以:

结尾
query_text += ' FROM mystock_stock GROUP BY productid_id'