I'm trying to dump my data to SQL statements. the django-admin.py dumpdata provides only json,xml,yaml. so:
does someone know a good way to do it?!
I tried that:
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'm going over the model values myself, and make the INSERT statement myself. then I thought of using "django-admin.py sql" to get the "CREATE" statement.. but then I don't know how to use this line from inside my code (and not through the command-line). I tried os.popen and os.system, but it doesn't really work.. any tips about that?
I'll put it clearly: how do you use the "manage.py sql " from inside your code?
I add something like this to my view:
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
the thing is - it works.. but it writes the statements to the stdout... it's something, but not perfect. I'll try using django.core.management.sql.sql_create directly, we'll see how it goes..
thanks
I suggest to use SQL-specific dump program (e.g. mysqldump for MySQL).
For sqlite embedded in Python, you can look this example not involving Django: