2

I'm starting a project with Django, previously I've used Yii and Rails and they have tools that will take a DB structure and build models for you based off the DB structure. From what I've read Django seems to be the other way round in that it builds the DB from what you put in your models.

While Django is best suited for developing new applications, it’s quite possible to integrate it into legacy databases. Django includes a couple of utilities to automate as much of this process as possible.

As someone new to Django how common/acceptable is it to build your DB first and then use inspectdb to generate the models? Also inevitably your DB structure will change as you go through development, what are the best practices to deal with this and making sure the layers stay in sync.

How does syncing the DB and Django affect data existing in the DB too

1

I'm not a Django expert (I've done one or two moderately sized projects and, years ago, did some Rails), but to the best of my knowledge:

As someone new to Django how common/acceptable is it to build your DB first and then use inspectdb to generate the models?

It's fine. The Django project provides and documents inspectdb, so there's nothing wrong with using it.

Also inevitably your DB structure will change as you go through development, what are the best practices to deal with this and making sure the layers stay in sync.

Current versions of Django provide migrations, just like Rails does. See the official docs or various tutorials, like this one. Basically:

  • Run ./manage.py makemigrations to create your migrations. Django will automatically create Python code to set up a database to match your Django models.
  • Run ./manage.py migrate to apply your migrations.
  • Whenever you've changed your models, run ./manage.py makemigrations. Django will automatically inspect your Django models, see what's changed since the last time it ran, and generate Python code to bring the database in sync with your model changes.

Note that, if you ran inspectdb, the generated models have managed=False, so they're excluded from Django's migrations logic. I really like migrations, so after I made sure that my inspectdb-created models are good, I would set managed=True and let Django manage the models from there on out. (I have no experience with this to know how well it works.)

How does syncing the DB and Django affect data existing in the DB too

Migrations modify the database in-place (as if you were manually issuing ALTER TABLE commands or whatever), so data is left alone where possible. Changes such as adding a column may require new default data (Django can prompt you if needed); changes such as dropping a column will obviously lose data.

  • Thanks for your answer Josh. That's good that if I were to add a column and it wasn't allowed to be NULL that Django would prompt me for default data. I would much rather design the DB first and then have the model generated based off the tables. I love Gii in Yii for this. Does it provide relations automatically when using inspectdb do you know? – Jonnny Dec 1 '16 at 18:26
  • @Jonnny - I assume that inspectdb will automatically provide relations, but I've never used it myself. – Josh Kelley Dec 1 '16 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.