tjati
tjati

Reputation: 6089

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.

Upvotes: 2

Views: 331

Answers (2)

François Constant
François Constant

Reputation: 5496

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

Upvotes: 1

falsetru
falsetru

Reputation: 369444

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)

Upvotes: 3

Related Questions