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, its  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
Django database modelling
database design;django
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 tooMigrations 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.