简体   繁体   中英

Django naming convention for views/models as package

I split my views.py and models.py into submodule views/ and models/

the module views.py is in plural. when it is split into submodule views/ the files have to be plural or singular?

example, in the models/:

models/
    employee.py
    department.py
    schedule.py

views/
    employee.py
    department.py
    schedule.py

according to django, the files should be in plural, but in such situation, should i give each file name plural or singular?

Update: There are many apps in our project. and here is one app that has many models with many interdependent, so it makes sense to belong to a single app. and we like things clean and nice, so we split them into submodule like views/ and models/.

This is not a solution you want.

But splitting the project into small django apps will make more sense over here.

I personally follow this structure and have seen many projects following the same

employee/
    models.py
    views.py

schedule/
    models.py
    views.py

department/
    models.py
    views.py

But your project structure is not allowing you to do then what you have added is also readable and makes sense.

I think naming model files in the singular would be reasonable, but with view-files it is worth thinking, because very often view can use several models inside itself, in such cases such file name can be misleading. I usually call view files based on the business logic they make.

It's also a good idea to look towards splitting the project into django apps.

I don't think there is any convention or guideline or for naming modules for views. It is more of a personal choice. Just pick singular or plural and use stick with it in on all places.

For picking a choice, you can see how you named your deepest layer in stack (model). If you have used Employee for your employee model (which is more likely), use employee for module. If you have used Employees use employees . If I was you, I would use singular name for module even if there are multiple classes (one for each of CRUD operation) in one file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM