Models in Django gets everything in one file?

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 even found strange in Django models being all together, is this normal? Is there a way to leave each model in a file? Is not the community strange?

    
asked by anonymous 12.12.2018 / 20:32

1 answer

3
  

Is it normal?

It's up to you. For small applications, you usually do not need too many files, you can simply put them all together in one.

  

Is there a way to leave each model in a file?

It has, but you will have much more work with it, because in Python the file is what defines a module. By creating a model in each file, you will be creating a module for each model . This does not make sense, as well as adding a large redundancy in the code:

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

The model name will end up repeating itself as the module name.

  

Does not the community like this?

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 have quoted do not use this format.

But, all in all, realize that not all models will be in the same file. Django works with the apps concept and each app has its own models file. 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 multiple apps and consequently multiple models     

12.12.2018 / 20:44