Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with multiple apps overriding management commands in Django

Tags:

python

django

I've got an issue where there's more than one app trying to override the same management command in a Django project.

  1. Are there sensible ways to deal with this?
  2. Which gets priority - the app the was defined first in INSTALLED_APPS, or the one that was defined last?
  3. Is it possible to effectively subclass the most recently defined management command rather than simply replacing it?

For context I'm trying to get django_pdb (see github) to work more nicely with south and django.contrib.staticfiles.

like image 933
Tom Christie Avatar asked Dec 29 '11 17:12

Tom Christie


2 Answers

2.5 years later, but in case someone has the same problem and lands here after a google search, I've made a small django app to deal with that case: django-mcmo ('Management Command Multiple Override'), available on pypi. It has limitations but works as expected.

Works with django 1.4 to 1.8 and py 2 and 3, contributions welcome on bitbucket repo.

like image 61
ouk Avatar answered Nov 13 '22 21:11

ouk


Easiest answer I'm aware of is: structure your project so you can change one of them and keep a record of your changes so you can apply it to future releases.

For my projects I like to have:

/myproject
  /lib
    /app1
    /app2
    /app3

Then explicitly add /lib to the path in setup.py

import os
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

import sys
lib_dir = os.path.join(PROJECT_PATH, 'lib')
if lib_dir not in sys.path[:4]:
    sys.path.insert(1, os.path.join(PROJECT_PATH, 'lib'))

I'm probably far more likely than average to take an app, install it, then change 10% of it to work exactly how I want.

The advantage of this is: 1) most dependencies ship with the code and are tracked in GIT 2) no chance for a system wide change to unexpectedly cause bugs in an app if you are running multiple apps from the same machine and 3) Easy to change, with revision history, any and everything in the app.

Not having dove too deeply into south's management commands, and never having used django_pdb, your particular problem might not be solved with the "make a local copy and rename one of them" approach, but I share in case it might.

like image 33
Ted Avatar answered Nov 13 '22 23:11

Ted