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.
Asked
Active
Viewed 1,513 times
1 Answers
3
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
inspectdb
to create models - Use
dumpdata
to save values as json file - Remove
managed = False
from your scaffolded models (from point 3) - Create migrations with
makemigrations
command.
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.

Igor
- 3,129
- 1
- 21
- 32
-
i don't get it. the sql file was created with php. i would like to load the data to postgresql with django. i do not remember the tables and column. – Kishan M Oct 12 '17 at 11:56
-
2You have SQL file, so you don't need to remember tables and columns. Just create the database and load dump (with PgAdmin for example). And then you can use inspectdb to create models. And you can use `manage.py dumpdata` to create JSON if you don't want to deploy SQL manually on server. I update the answer with steps guide – Igor Oct 12 '17 at 12:27