Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database first Django models

In ASP.NET there is entity framework or something called "database first," where entities are generated from an existing database. Is there something similar for Django?

I usually work with a pre-existing database that I need to create a backend (and subsequently a front end) for. Some of these relational databases have many tables and relations so manually writing models isn't a good idea. I've scoured Google for solutions but have come up relatively empty handed.

like image 840
fpes Avatar asked May 29 '15 00:05

fpes


2 Answers

You can use the information on this link.

python manage.py inspectdb > models.py

https://docs.djangoproject.com/en/3.0/howto/legacy-databases/ It depends on your database, but I've worked with it and is good.

like image 106
imarban Avatar answered Sep 29 '22 12:09

imarban


The default in Django is a Code First-type approach where the Django framework engine creates a Db for you based on your models and uses migrations to update the Db with model changes (like Code First). https://docs.djangoproject.com/en/1.11/intro/tutorial02/

What you're describing sounds like a Database First approach in the .Net world. Integrating Django with a legacy database: https://docs.djangoproject.com/en/1.11/howto/legacy-databases/

like image 39
anacrust Avatar answered Sep 29 '22 13:09

anacrust