How to override an app in Django properly?

2.3k views Asked by At

I'm running Satchmo. There are many apps and I've changed some of the source in the Product app.

So my question is how can I override this properly because the change is site specific. Do I have to copy over the whole Satchmo framework and put it into my project or can I just copy one of the apps out and place it in say Satchmo>App>Products? (Kinda like with templates)

Thanks

3

There are 3 answers

0
darren On BEST ANSWER

What I have done which works is to copy the application that I have changed. In this case satchmo\apps\product. I copied the app into my project folder Amended my setting.py INSTALLED_APPS from 'product', to 'myproject.product',

This now carries the changes I've made to this app for this project only and leaves the original product app untouched and still able to be read normally from other projects.

3
Laur Ivan On

Normally, I'd say the best thing is to fork Satchmo and keep a copy with your changes.

If you are willing to play with the python path, make sure that your app's directory appears before the other (default) directory. From my tests, if you have two apps/modules with identical names, the first one found is used.

2
Chris W. On

When you add a 'Django App' to INSTALLED_APPS in your settings.py file, you're telling Django that there exists an importable python module with that name on your "python path". You can view your python path by viewing the contents of the list stored at sys.path.

Whenever Python (and in this case Django) attempts to import a module it checks each of the directories listed in sys.path in order, when it finds a module matching the given name it stops.

The solution to your problem then is too place your customized Django Apps, e.g., the Satchmo product module, into a location in your python path which will be checked before the "real" Satchmo product module.

Because I don't know how you have your directory structure laid out I'm basically making a guess here, but in your case, it sounds like you have the Satchmo apps living somewhere like /satchmo/apps/ and your project at /my_custom_path/my_project/. In which case you might want to add your customized product module to /my_custom_path/my_project/product/. Because the path at which your Django settings.py file lives is always checked first, that should mean that your customized product module will be found first and imported instead of the built in Satchmo one.

FYI: To check and see the order in which your Satchmo installation is checking directories for modules run python manage.py shell and then in the prompt do import sys; print sys.path.