Django-admin.py dumpdata for SQL statements

I am trying to dump my data into SQL statements. dumpdata django-admin.py provides only json, xml, yaml. So:

  • Does anyone know how to do this?

  • I tried:

    def sqldumper (model):

    result = ""
    units = model.objects.all().values()
    for unit in units:
        statement = "INSERT INTO myapp.model "+str(tuple(unit.keys())).replace("'", "")+" VALUES " + str(tuple(unit.values()))+"\r\n"
        result+=statement
    return result
    

so I go over to model values ​​myself and do the INSERT statement myself. then I thought about using "django-admin.py sql" to get the "CREATE" statement .. but then I don’t know how to use this line from within my code (and not through the command line). I tried os.popen and os.system, but actually it does not work. any advice on this?

I will clearly state: how do you use "manage.py sql" from your code?

I am adding something like this to my opinion:

import os, sys
import imp
from django.core.management import execute_manager

sys_argv_backup = sys.argv
imp.find_module("settings")
import settings
sys.argv = ['','sql','myapp']
execute_manager(settings)
sys.argv = sys_argv_backup

- ​​.. stdout... -, . django.core.management.sql.sql_create , , .

+3
1

SQL- - (, mysqldump MySQL).

sqlite, Python, , Django:

# Convert file existing_db.db to SQL dump file dump.sql
import sqlite3, os

con = sqlite3.connect('existing_db.db')
with open('dump.sql', 'w') as f:
    for line in con.iterdump():
        f.write('%s\n' % line)
+3

All Articles