Search code examples
djangodjango-modelsdjango-viewspython-module

Is it possible to use the same Django application name in multiple locations within a project?


I have a similar project structure to one below.

project_name/
│
├── apps/
│   ├── items/
│   │   ├── purchases/
│   │   │   ├── migrations/
│   │   │   ├── templates/
│   │   │   ├── __init__.py
│   │   │   ├── admin.py
│   │   │   ├── apps.py
│   │   │   ├── models.py
│   │   │   ├── tests.py
│   │   │   └── views.py
│   │   │
│   │   └── sales/
│   │       ├── migrations/
│   │       ├── templates/
│   │       ├── __init__.py
│   │       ├── admin.py
│   │       ├── apps.py
│   │       ├── models.py
│   │       ├── tests.py
│   │       └── views.py
│   │
│   └── tools/
│       ├── purchases/
│       │   ├── migrations/
│       │   ├── templates/
│       │   ├── __init__.py
│       │   ├── admin.py
│       │   ├── apps.py
│       │   ├── models.py
│       │   ├── tests.py
│       │   └── views.py
│       │
│       └── sales/
│           ├── migrations/
│           ├── templates/
│           ├── __init__.py
│           ├── admin.py
│           ├── apps.py
│           ├── models.py
│           ├── tests.py
│           └── views.py
│
├── project_name/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
│
└── manage.py

settings

INSTALLED_APPS=[
...
"items.purchases",
"items.sales",
...
"tools.purchases",
"tools.sales",
...
]

I am getting the following exceptions

django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: purchases

items/purchases/apps.py

from django.apps import AppConfig


class PurchasesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'items.purchases'

tools/purchases/apps.py

from django.apps import AppConfig


class PurchasesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'tools.purchases'

Is it okay to use the same Django app name in multiple places within a project, or would it be better to rename them to prevent conflicts? Additionally, is it considered a good practice to add a trailing underscore to app names to differentiate between conflicting names?


Solution

  • The docs shows it's not a good practice. It says:

    Application names and labels must be unique in INSTALLED_APPS

    Application names — the dotted Python path to the application package — must be unique. There is no way to include the same application twice, short of duplicating its code under another name.

    Application labels — by default the final part of the name — must be unique too. For example, you can’t include both django.contrib.auth and myproject.auth. However, you can relabel an application with a custom configuration that defines a different label.

    These rules apply regardless of whether INSTALLED_APPS references application configuration classes or application packages.