S.Singh
S.Singh

Reputation: 31

Django model with number of fields less than corresponding database table

If a database table contains 100 fields, and a django application utilises only a few fields say 1 or 2, does the corresponding django model needs to be declared with 100 fields?

Upvotes: 3

Views: 502

Answers (2)

thebjorn
thebjorn

Reputation: 27321

No, you do not need to declare the Django model with all the fields from the database.

Upvotes: 2

Abijith Mg
Abijith Mg

Reputation: 2693

Django comes with a utility called inspectdb that can create models by introspecting an existing database

python manage.py inspectdb

Save this as a file by using standard Unix output redirection:

python manage.py inspectdb > models.py

This will reduce the stress of manually defining the model.

Ref: https://docs.djangoproject.com/en/1.10/howto/legacy-databases/

Upvotes: 0

Related Questions