Answer the question
In order to leave comments, you need to log in
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
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.
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question