2

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.

tjati
  • 5,761
  • 4
  • 41
  • 56
  • For the sake of anyone maintaining your project afterwards, I would strongly suggest that you stick to the conventions and use `models.py` `urls.py` and `views.py`. If organisation is an issue you can use `models` and `views` subfolders but I would really recommend sticking with the nomenclature – Timmy O'Mahony Nov 03 '14 at 14:21

2 Answers2

3

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)

falsetru
  • 357,413
  • 63
  • 732
  • 636
1

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

Community
  • 1
  • 1
François Constant
  • 5,531
  • 1
  • 33
  • 39