【问题标题】:Raw query must include the primary key in custom posgresql function原始查询必须包含自定义 postgresql 函数中的主键
【发布时间】:2019-06-25 02:09:38
【问题描述】:

我用 pg/sql 语言做了这个功能,例如:dinamico('some_string') 我在一个简单的 django 视图中使用:

def detalle_fondo(request,fondo):
    det_fondos=f.objects.raw('select * from dinamico(%s)',[fondo])
    return render(request,'sw/det-fondos.html',{'det_fondos':det_fondos})

问题是django问我主键:

原始查询必须包含主键

但我现在不知道如何通过自定义函数传递主键。 你能帮帮我吗?

【问题讨论】:

  • 什么是dinamico?为什么这个原始 sql 链接到 model.objects

标签: django python-3.x


【解决方案1】:

遵循以下方法

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute('select * from dinamico(%s)', [fondo])
        for row in cursor.fetchall():
            print(row)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 2014-01-21
    • 2020-05-09
    • 2021-02-19
    • 1970-01-01
    • 2018-11-14
    • 2017-06-29
    • 1970-01-01
    相关资源
    最近更新 更多