1

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.

Kishan M
  • 11
  • 4

1 Answers1

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.

  1. Deploy the temporary DB and load dump with some tool like PgAdmin.
  2. Setup your django app so it can connect to the same DB
  3. Use inspectdb to create models
  4. Use dumpdata to save values as json file
  5. Remove managed = False from your scaffolded models (from point 3)
  6. 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
  • 2
    You 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