简体   繁体   中英

In django It's correct create much models into file models.py

I am create a new web application with django but I have one question, if I need create 30 o 40 tables into data base, I need put all models into file models.py?

So I think that this is very complicated of maintain, because this file may growa lot

This is my question, is optimum make that?

You can create a models folder and have more specific files for all your models instead of just the single models.py file.

Say if you have 10 tables relating to the user and 10 more relating to a blog, instead of keeping them in models.py you can create a models folder and create a user.py file inside that folder, doing the same with all the blog models but in a blog.py file.

File structure would move from this:

app/
 |
  models.py

To this:

app/
  |
   models/
     |
      __init__.py
      user.py
      blog.py

Don't forget the include the __init__.py file in the models folder or else python won't recognize it as a package and you won't be able to import the models into other files.

Also you now import the models by doing from app.models.user import someModel instead of from app.models import someModel .

You can follow this same structure for all the files in Django such as views.py, forms.py, settings.py, etc..

所有表都创建到models.py中,所有查询都可以写到views.py中

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