django-herokuapp icon indicating copy to clipboard operation
django-herokuapp copied to clipboard

clarify/document dotted paths for loading apps

Open puterleat opened this issue 10 years ago • 4 comments

Apologies if this is a non-issue, but I'm wondering about how apps should be references from the settings.py files. It seems odd to me that I have to write myprojectname.apps.myappname when new app folders are located there. On the one hand it's explicit, but on the other it seems less portable (e.g. if one app were to want to refer to another)... How do you reference an app from the settings file?

puterleat avatar Oct 15 '14 20:10 puterleat

You should always use the full, absolute path to a django app.

So, in settings.py:

“myprojectname.apps.myappname”

From one app, importing another:

from myprojectname.apps.myappname.models import SomeModel

How is this not portable?

On 15 Oct 2014, at 21:19, puterleat [email protected] wrote:

Apologies if this is a non-issue, but I'm wondering about how apps should be references from the settings.py files. It seems odd to me that I have to write myprojectname.apps.myappname when new app folders are located there. On the one hand it's explicit, but on the other it seems less portable (e.g. if one app were to want to refer to another)... How do you reference an app from the settings file?

— Reply to this email directly or view it on GitHub.

etianen avatar Oct 16 '14 15:10 etianen

If you wanted to extract an app into a pluggable app later for example? Or if you wanted to create a new instance/fork of myprojectname with a different name.

Adding the apps directory to the python path would be slightly magic, but would simplify this and make apps within the project equivalent to apps installed via pip for example.

On 16 Oct 2014, at 16:11, Dave Hall [email protected] wrote:

You should always use the full, absolute path to a django app.

So, in settings.py:

“myprojectname.apps.myappname”

From one app, importing another:

from myprojectname.apps.myappname.models import SomeModel

How is this not portable?

On 15 Oct 2014, at 21:19, puterleat [email protected] wrote:

Apologies if this is a non-issue, but I'm wondering about how apps should be references from the settings.py files. It seems odd to me that I have to write myprojectname.apps.myappname when new app folders are located there. On the one hand it's explicit, but on the other it seems less portable (e.g. if one app were to want to refer to another)... How do you reference an app from the settings file?

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

puterleat avatar Oct 16 '14 15:10 puterleat

If you want to install custom apps in the root of your project, that’s absolutely fine. The reason this project structure is suggested is that it namespaces all the project apps, avoiding collisions with built-in python modules. Otherwise you can’t make an app called “site”, for example, since it would clash with a builtin.

In my experience, apps are very rarely extracted into pluggable apps, and when they are, it’s not a great deal of effort to find and replace a few pathnames. For the 99% of cases where project-specific apps are never extracted into pluggable apps, having a namespace is good for avoiding naming collisions.

On 16 Oct 2014, at 16:17, puterleat [email protected] wrote:

If you wanted to extract an app into a pluggable app later for example? Or if you wanted to create a new instance/fork of myprojectname with a different name.

Adding the apps directory to the python path would be slightly magic, but would simplify this and make apps within the project equivalent to apps installed via pip for example.

On 16 Oct 2014, at 16:11, Dave Hall [email protected] wrote:

You should always use the full, absolute path to a django app.

So, in settings.py:

“myprojectname.apps.myappname”

From one app, importing another:

from myprojectname.apps.myappname.models import SomeModel

How is this not portable?

On 15 Oct 2014, at 21:19, puterleat [email protected] wrote:

Apologies if this is a non-issue, but I'm wondering about how apps should be references from the settings.py files. It seems odd to me that I have to write myprojectname.apps.myappname when new app folders are located there. On the one hand it's explicit, but on the other it seems less portable (e.g. if one app were to want to refer to another)... How do you reference an app from the settings file?

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub.

etianen avatar Oct 16 '14 15:10 etianen

OK yes - that makes sense. I hadn’t thought about the collision in names.

On 16 Oct 2014, at 16:22, Dave Hall [email protected] wrote:

If you want to install custom apps in the root of your project, that’s absolutely fine. The reason this project structure is suggested is that it namespaces all the project apps, avoiding collisions with built-in python modules. Otherwise you can’t make an app called “site”, for example, since it would clash with a builtin.

In my experience, apps are very rarely extracted into pluggable apps, and when they are, it’s not a great deal of effort to find and replace a few pathnames. For the 99% of cases where project-specific apps are never extracted into pluggable apps, having a namespace is good for avoiding naming collisions.

puterleat avatar Oct 16 '14 15:10 puterleat