Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override an app in Django properly?

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

like image 373
darren Avatar asked Feb 27 '11 19:02

darren


2 Answers

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.

like image 120
darren Avatar answered Oct 24 '22 19:10

darren


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.

like image 40
Chris W. Avatar answered Oct 24 '22 21:10

Chris W.