0

I want to generate SQL code and take from that code, generating a Django model to avoid errors. They will say that you first create the model and run the syncdb or migrate but my case is unlike the database is already created and I now want the model

developer
  • 21
  • 4

1 Answers1

1

Run this command to auto-generate models from an already existing database. But first make sure you've properly linked database to django app .

python manage.py inspectdb > models.py

Do check models.py file and make some changes if you something isn't rendered correctly.

For inspectdb approach, read this: https://docs.djangoproject.com/en/1.8/howto/legacy-databases/

Alternatively, you can write all the models by yourself and set managed = False. No database table creation of deletion will be executed by Django on this model. But it is somewhat complicated and puts some limits on model relationships.

For managed=False approach, read this: https://docs.djangoproject.com/en/1.8/ref/models/options/#managed

narendra-choudhary
  • 4,582
  • 4
  • 38
  • 58