MySQL 中带有存储过程的 Django 表单

Django forms with stored procedure in MySQL

我正在尝试调用 Django 表单中的存储过程。这将在模板中填充 select。显然它可以用 cursor.execute () 调用,但我无法让它工作。

我留下部分代码:

class FormX(forms.ModelForm):
    fieldX = forms.ModelChoiceField(queryset=cursor.execute('SELECT * FROM table'), required=True)
    

您可以尝试使用 documentation 执行原始查询 而another one直接使用

如果你想使用游标

from django.db import connection

    def my_custom_sql(self):
        with connection.cursor() as cursor:
            cursor.execute("SELECT * FROM table")
            row = cursor.fetchall()
        return row