3

This question makes in part reference to Django directory structure? .

How should I place the models.py in Django?

Project_name/
    Application1/
        models.py
    Application2/
        models.py

or

Project_name/
    DB/
        models.py

Which one should I use? why?

Community
  • 1
  • 1
zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • 1
    Django creates the models file for you when you do `manage.py startapp`. Why do you want to move it? – Daniel Roseman Jul 09 '13 at 16:18
  • @DanielRoseman I don't mean moving it, but creating a new one with all them in just one file. It looks cleaner in my opinion. – zurfyx Jul 09 '13 at 16:19

2 Answers2

2

The first option is correct in Django each app has her own models.py or models directory.

zurfyx
  • 31,043
  • 20
  • 111
  • 145
2

Daniel Roseman is correct, when running manage.py startapp appname, Django will automatically create a directory structure with a separate models.py per app like:

appname/
    models.py

As well as being convention (if other Django developers come to look at your code, this is where they will expect to find your models) it also allows people to create reusable apps

e.g. by simply copying the appname directory I have all the URL patterns (if any), views, models, forms, tests and everything else, rather than having to pull out all the files from a large separate directory.

Ewan
  • 14,592
  • 6
  • 48
  • 62