1 Create and Run stored procedures
Sicong редактировал(а) эту страницу 2021-08-25 09:05:12 -07:00
  1. Create a stored procedure in migrations
    In yourapp/migrations create a custom migration file.
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('mytestapp', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL("""CREATE PROCEDURE mySP
                             ...
                          """
       ),
    ]
$ python manage.py migrate
  1. The stored procedure can be called with a raw SQL query.
from django.db import connection

def run_mySP():
    with connection.cursor() as cursor:
        cursor.execute("EXEC mySP;")
        result = cursor.fetchall()
    return result