【问题标题】:a raw sql query is not working in django framework. I am getting "TypeError at / not all arguments converted during string formatting"原始 sql 查询在 django 框架中不起作用。我收到“TypeError at / not all arguments 在字符串格式化期间转换”
【发布时间】:2020-04-18 18:46:40
【问题描述】:

select *from table_name where name = %s",params={"name1","name2"}

完整代码:

from django.shortcuts import render
from . models import destination

def index(request,params = None):       
  dests1 = destination.objects.raw("select *from travello_destination where name = %s",params={'Bangalore','Mumbai'})
  return render(request,'index.html', {"dests":dests1})

【问题讨论】:

    标签: django rawsql


    【解决方案1】:

    您错误地使用了原始query,来自文档:

    params 是参数列表或字典。您将使用 %s 列表查询字符串中的占位符,或 %(key)s 占位符 对于字典(其中键被字典键替换, 当然),无论您的数据库引擎如何。这样的占位符将是 替换为 params 参数中的参数。

      dests1 = destination.objects.raw(
       "select *from travello_destination where name = %s or name=%s", params=['Bangalore','Mumbai'])
    

    另外我建议你使用ORM 而不是原始查询

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      相关资源
      最近更新 更多