Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I place models.py in Django?

Tags:

python

django

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?

like image 916
zurfyx Avatar asked Jan 21 '26 14:01

zurfyx


1 Answers

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.

like image 59
Ewan Avatar answered Jan 24 '26 03:01

Ewan