Use cases for new Django 1.4 project structure?

2019-07-25 01:40发布

问题:

I guess this is sort of a followup question to Where should i create django apps in django 1.4? The final answer there seemed to be "nobody knows why Django changed the project structure" -- which seems a bit unsatisfactory.

We are starting up a new Django project, and currently we are following the basic structure outlined at http://www.deploydjango.com/django_project_structure/index.html:

├── project
│   ├── apps
│   │   ├── app1
│   │   └── app2
│   ├── libs
│   │   ├── lib1
│   │   └── lib2
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

But I think we also are anticipating a multi-developer environment comprising largely independent applications with common project-level components, so it seems cleaner to me to separate out the project and app paths.

├── project
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── apps
│   ├── app1
│   └── app2
├── libs
│   ├── lib1
│   └── lib2
└── manage.py

It's hard to come up with any specific, non-stylistic rationale for this, though. (I've mostly worked only with single-app projects before now, so I may be missing something here.)

Mainly, I'm motivated by the fact that Django 1.4 seems to be moving in the latter direction. I assume there is some rationale or expected use case that motivated this change, but I've only seen speculation on what it might be.

Questions:

  1. What was the motivation for the 1.4 project structure change?
  2. Are there use cases where having apps inside/outside of the project makes a non-trivial impact?

回答1:

  1. It's much easier to extract an app from a project because there are no more imports like this:

    from projectname.appname.models import MyModel
    

    instead you import them the same way you would import apps which are installed via a python package

  2. if you use i18n then this could make an impact because makemessages searches for translation strings in the current directory. a good way to translate apps and the project using a single .po file is to create the locale folder outside the project dir

    ├── project
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── app1
    ├── app2
    ├── locale
    │   ├── en
    │   └── de
    └── manage.py
    


回答2:

I marked the earlier response as an answer, but I ran into this blog post off the IRC archives that seems to have some additional info.

http://blog.tinbrain.net/blog/2012/mar/15/django-vs-pythonpath/

As I understand it, the gist is:

  • When you're developing, manage.py implicitly sets up PYTHONPATH to see the project-level code, with the result that import myapp works for an app defined inside the project.
  • When you deploy, you generally don't run manage.py, so you would have to say import myproject.myapp, thus things break on deployment if you don't know about this.
  • The "standard" fix is to add the project to PYTHONPATH, but this results in double-imports (myapp and myproject.myapp), which can generate weird behavior on things like signals.

So the 1.4 project structure seems mainly intended to eliminate the possibility of devs relying on an odd effect of manage.py.