How to migrate Django models from mysql to sqlite (or between any two database systems)?

℡╲_俬逩灬. 提交于 2019-12-03 13:10:01

问题


I have a Django deployment in production that uses MySQL.

I would like to do further development with SQLite, so I would like to import my existing data to an SQLite database. I

There is a shell script here to convert a general MySQL dump to SQLite, but it didn't work for me (apparently the general problem isn't easy).

I figured doing this using the Django models must be much easier. How would you do this? Does anyone have any script to do this?


回答1:


use

manage.py dumpdata > your_file.json

to export your data from the production system (docs).

Then move the file on the development system and run

manage.py loaddata your_file.json

You can also put the file in your_app/fixtures folder with name "initial_data.json" and it will be automatically loaded when you run "manage.py syncdb" (docs).




回答2:


Have you tried using manage.py dumpdata > datadump and then when the new database is set up correctly, use python manage.py loaddata datadump?




回答3:


If you have contenttypes in installed app

INSTALLED_APPS = (
    'django.contrib.contenttypes',
)

Use script like what for copying you entry to new base:

from django.contrib.contenttypes.models import ContentType

    def run():

        def do(Table):
            if Table is not None:
                table_objects = Table.objects.all()
                for i in table_objects:
                    i.save(using='slave')

        ContentType.objects.using('slave').all().delete()

        for i in ContentType.objects.all():
            do(i.model_class())

See full manual here




回答4:


Maybe give south a try:

http://south.aeracode.org/docs/index.html



来源:https://stackoverflow.com/questions/4544419/how-to-migrate-django-models-from-mysql-to-sqlite-or-between-any-two-database-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!