C
C
Cube2016-04-10 16:14:49
MySQL
Cube, 2016-04-10 16:14:49

How to port data from *.sql dump to database and then import it into Django?

Good afternoon.
There was a project in which the "news" application was located, respectively, a database with the news_post table was created in mysql. For several months, all the data accumulated. I urgently needed to save all this data and I made a database dump.
Then the "blog" application was added to the already existing "news" application, but it's all on a different server.
Actually the question itself:
How can I restore the data from the .sql file to the database and pick it up with django so that the "news" application displays the "old" data, and the "blog" remains empty until I add new data?
I did:
mysq -u -p example_db < file.sql
but django doesn't import them. Rather, I do not understand how to do it.
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cube, 2016-05-26
@liveunit

Decided in this way:
1.Created an empty database.
2. Made syncdb.
3.And then did mysql -u -p example_db < file.sql.
Django picked up everything, the data was added to the database.

M
Mintormo, 2016-04-10
@Mintormo

It was necessary to do a dump using Django, not MySQL. To do this, there is a command manage.py dumpdata which makes a dump in JSON/YAML/XML formats. You can make a dump of a specific application and then use it as initial data. This mechanism is called fixtures.
Read. And here's how to transfer SQL to JSON, YAML or XML ... There you need to not just transfer it, but with a certain structure so that Django picks it up. The first thought (most likely unsuccessful) is to create a temporary database and upload the data from the SQL dump there. Then write a python script that will create a file in JSON / YAML / XML format with the correct structure, taking data from the temporary base. Then just load it with manage.py loaddata. That's such a horror :)). Surely there are easier options, but I'm too lazy to dig.

Z
zelsky, 2016-04-10
@zelsky

Check models and fields accordingly.
appname_basefieldname

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question