POO using Django

2

I'm starting to work with Django, but I have some questions about how to organize my project, I'd like to know how best to structure my projects.

I'm used to the languages in which I model each entity with its attributes and methods in a class that stays in just one file, however in Django what I see is that I just have a view inside that I define several classes. Is this the right way?

I saw that some people create an App for each entity, this would be the one that gets closer to what I'm used to, but is it a correct way?

    
asked by anonymous 30.10.2015 / 16:38

1 answer

4

No, it's not the right way.

Put simply, django works with apps. Every app you create on your system, should follow the context of something. For example: If on my system I have the following models:

Cliente
Produto
CategoriaProduto
Venda
ItensVenda

In this case, you can have 3 apps in the system:

  • Customers #app clients
    • Client
  • Products # app products
    • Product
    • Product Category
  • Sales # app sales
    • Sale
    • Sales Items

You can put 50 models in an app the same way you can put 1 model in an app. Two Scoops of Django says that if you have more than 5 models in one app, you should split your app in two. If you have a very generic template that can be used on multiple systems, you can have an app just for it. This will make your life much easier. For example: "Category" is something that can be used in various systems, what changes is the context of the category. In a sales system is product category, in a service system is the type of service ..

It is not something that should be taken care of, but you have to keep in mind that every thing should be in its place. And working in a modular way, means that everything must be in its proper place. This will help in refactoring and maintaining the code.

I've had the misfortune to work on codes that old developers created a single "core" app and played all the models of the project in there. This is very bad because you have thousands of things stored in the same place. Then to see something in the project, upload file ... down file .. go up again to see if it mattered.

On the view question, django works with the "MVT" structure, Model, View, and Template. In that medium you have your forms too.

I like to explain this in a very basic way:

  • Model: Responsible for the system classes, the screens that will go to the database.
  • Forms: Responsible for "what" will be presented and how it will be presented (referring to models).
  • View: Responsible for sending and receiving your requests.

I can not say that it is wrong to say that you have classes in your views because if you are working with Class Based View, you will have classes in your views. If it is in this way, I recommend following initially by Function Based View to later jump to CBV.

About the project architecture itself, you can organize it like this:

project/
    clientes/
        models.py
        views.py
        ...
    produtos/
    project/
        settings.py
        urls.py
        ....
    vendas/

A well-modularized design and good practice is incredibly easy to read and understand. You should make your code so that if another developer works on it, it will see that you care about the quality of the code.

    
31.10.2015 / 04:58