How to override an app in Django properly?
Asked Answered
D

3

6

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

Dido answered 27/2, 2011 at 19:45 Comment(0)
D
5

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.

Dido answered 8/3, 2011 at 13:21 Comment(0)
B
4

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.

Benedikt answered 28/2, 2011 at 3:0 Comment(2)
Hiya Chris. But what if I don't want my other apps to use this custom Product app? They will all still import this Product first as it's found first on the path. I want to override the app for this site only.Dido
Perhaps if you explain the layout of your directory structure I'll be able to help. As of now, it's unclear to me how you have your apps, libraries and projects laid out.Benedikt
C
2

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.

Concordant answered 27/2, 2011 at 20:24 Comment(3)
heya. it's a site specific change so i don't really want to fork satchmo and place it on the path because i don't want any other project to use this.Dido
Then the second option stands. All you have to do is tweak the python path (as per Chris's notes) and make sure your "product" gets to be before the standard satchmo's product (which) I assume is installed via pip/easy_setup.Concordant
Also (I can't get it to do paragraphs), you could isolate your site with virtualenv. Then, only your site would have access to the modified satchmo copy...Concordant

© 2022 - 2024 — McMap. All rights reserved.