[Answered ]-Using Django models with an already created DB table

1👍

Yes. In fact Django can even help you create the models. Models that you do not migrate with the help of Django are unmanaged models. These have a managed = False attribute in the Meta class, so something like:

class MyModel(models.Model):
    # … fields …
    
    class Meta:
        managed = False

If you thus write these unmanaged models, you can make queries with the Django ORM, without Django trying to create new models for these tables.

Of course, specifying models that match with the database is cumbersome. Therefore Django can often construct models based on the tables. You can generate the models with the inspectdb command [Django-doc].

You can generate these models on the stdout with:

python3 manage.py inspectdb

or you can save these to a file through I/O redirection:

python3 manage.py inspectdb > app_name/models.py

Leave a comment