Mo J. Mughrabi
Mo J. Mughrabi

Reputation: 6997

building reusable package in django

Am trying to build a reusbale package for django, the package contains multiple application since the project is quite large.

So, the structure, has am apps folder and inside it there is multiple apps payment, product, account..etc

As am trying to make the project re-usable, I created it in a different location of the source project and did a symbolic link to a root folder in the source project.

Now, when I want to install the apps, I have to write them one by one in the INSTALLED_APPS

INSTALLED_APPS = ('PACKAGE.apps.store',
                  'PACKAGE.apps.product',
                  'PACKAGE.apps.delivery',
                  'PACKAGE.apps.payment',
                  'PACKAGE.apps.store',
                  'PACKAGE.apps.cart',
                  'PACKAGE.apps.tax')

Is there a way, where I can only include 'PACKAGE' in my installed apps and perhaps use the package init to load the other modules?

I tried doing something like this in PACKAGE/__init__.py

from django.conf import settings as user_settings
from PACKAGE.conf import settings
user_settings.INSTALLED_APPS = user_settings.INSTALLED_APPS + settings.INSTALLED_APPS

But it didn't work, any one can advise on this?

Upvotes: 0

Views: 82

Answers (1)

DavidM
DavidM

Reputation: 1437

I believe that once the "from django.conf import settings" line has executed, settings are effectively immutable.

What I would do is invert the logic a bit. In PACKAGE/__init__.py. Something like:

def get_apps():
    apps = (
        'apps.store',
        'apps.other',
        ...
    )
    return [__name__ + '.' + x for x in apps]

Then just:

INSTALLED_APPS += get_apps()

in settings.py. I do this quite a bit to keep our settings.py manageable and it seems to work quite well.

Upvotes: 1

Related Questions