Models in Django is everything in one file?

Asked

Viewed 47 times

1

I’m starting my studies in Django, I come from frameworks like Rails, Laravel, Asp.Net, and I came across a difference that I was surprised that in Django the models are all together, is that normal? Is there a way to leave each model in a file? The community doesn’t miss this?

1 answer

3


It is normal?

It’s up to you. For small applications, you usually don’t need many files, you can simply put them all together into one.

There is a way to leave each model in a file?

There is, but you will have much more work with it, because in Python the file defines a module. Creating a model in each file, you will be creating a module for each model. This makes no sense, besides adding a large redundancy in the code:

import foo.models.usuarios.Usuarios
import foo.models.cidades.Cidades
...

The name of model end up repeating itself as module name.

The community doesn’t miss it?

No, because this is the philosophy of Python. A directory defines a package (package) and a file defines a module (module). Many other languages you quoted do not use this format.

But on top of everything, realize that all the models will be in the same file. Django works with the concept of apps and each app has its own archive of models. That is, only the models that are related and belong to the same app that will be in the same file. If your application grows, you will end up having several apps and, consequently*, several models.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.