I am practising to develop a complex app, i have previously developed the same with PHP and have a sql file. i'd like to know how do i create new models and populate the data from the sql file. I have read tutorials about grabbing from JSON files, But is there any online resources for the same.
1 Answer
You can use manage.py inspectdb command.
It will connect to the DB and create model classes.
So you need to create the database manually and use DB tools to read structure and data from sql file. After that you should put connection settings into your settings.py file and execute:
$ python manage.py inspectdb > models.py
Doc - Integrating with legacy database
UPD as I understand you want to move from your SQL file to django-only solution. So this is how you can achieve it.
- Deploy the temporary DB and load dump with some tool like PgAdmin.
- Setup your django app so it can connect to the same DB
- Use
inspectdbto create models - Use
dumpdatato save values as json file - Remove
managed = Falsefrom your scaffolded models (from point 3) - Create migrations with
makemigrationscommand.
After that you will be able to deploy your application with Django tools: migrations to create DB structure and loaddata to load data from json.
2 Comments
manage.py dumpdata to create JSON if you don't want to deploy SQL manually on server. I update the answer with steps guide