简体   繁体   中英

Is the naming convention in django mandatory?

In Django you name your files like models.py , views.py , urls.py and so on. I wonder, if this naming convention is mandatory for Django. Will Django's functionality break if you place your models in a file called foo.py ? I mean, only the import-line should change, right? Or is there any magic with this named files done by the framework?

Of course, I won't give my files shitty names; but I am just curious.

views, urls can be configured.

  • url: You can defined your one urls by setting <project>.settings.ROOT_URLCONF , and include your apps' urls.
  • views: Import your views or use view names as you want.

But for models, there's assumption about the model module name in the django code and other third-party apps. ( https://github.com/django/django/blob/stable/1.7.x/django/apps/config.py#L9 )

The best is to create modules instead for a couple of reasons:

  • you'll keep it consistent with Django conventions making it easier for others to work on it
  • you can give more descriptive appropriate names to your files
  • you will avoid really long files

So you'd have:

- my_application
  - urls
    - sub_set_urls_1.py
    - etc.
  - models
    __init__.py <= import your models in here
    sub_set_models_1.py
    etc.
  - views
    sub_set_views_1.py
    etc.

It's a bit more involved for the models , you need to import the models in __init__.py . Another way is to have a models.py file and put your models somewhere else: Split models.py into several files

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