Many parts of Oscar can be adapted to your needs like any other Django application:
- Many settings control Oscar’s behavior
- The looks can be controlled by extending or overriding the templates
But as Oscar is built as a highly customisable and extendable framework, it doesn’t stop there. The behaviour of all Oscar apps can heavily be altered by injecting your own code.
To extend the behavior of an Oscar core app, it needs to be forked, which is achieved with a simple management command. Afterwards, you should generally be able to override any class/model/view by just dropping it in the right place and giving it the same name.
In some cases, customising is slightly more involved. The following how-tos give plenty of examples for specific use cases:
- How to customise models
- How to add views or change URLs or permissions
- How to customise an existing view
For a deeper understanding of customising Oscar, the following documents are recommended:
Fork the Oscar app¶
If this is the first time you’re forking an Oscar app, you’ll need to create a root module under which all your forked apps will live:
$ mkdir yourappsfolder $ touch yourappsfolder/__init__.py
Now you call the helper management command which creates some basic files for you. It is explained in detail in Forking an app. Run it like this:
$ ./manage.py oscar_fork_app order yourappsfolder/ Creating folder apps/order Creating __init__.py and admin.py Creating models.py and copying migrations from [...] to [...]
For forking app in project root directory, call
. (dot) instead of
./manage.py oscar_fork_app order yourproject/
order app will be placed in
./manage.py oscar_fork_app order .
order app will be placed in
Replace Oscar’s app with your own in
You will need to let Django know that you replaced one of Oscar’s core
apps. You can do that by replacing its entry in the
with that for your own app.
Overrides of dashboard applications should follow overrides of core applications (basket, catalogue etc), since they depend on models, declared in the core applications. Otherwise, it could cause issues with Oscar’s dynamic model loading.
INSTALLED_APPS = [ # all your non-Oscar apps ... # core applications 'yourappsfolder.catalogue', 'yourappsfolder.order', # dashboard applications 'yourappsfolder.dashboard', 'yourappsfolder.dashboard.orders', 'yourappsfolder.dashboard.reports', ]
You can now override every class (that is dynamically loaded, which is almost every class) in the app you’ve replaced. That means forms, views, strategies, etc. All you usually need to do is give it the same name and place it in a module with the same name.
Suppose you want to alter the way order numbers are generated. By default,
oscar.apps.order.utils.OrderNumberGenerator is used. So just
create a class within your
order app which
matches the module path from oscar:
could subclass the class from Oscar or not:
# yourproject/order/utils.py from oscar.apps.order.utils import OrderNumberGenerator as CoreOrderNumberGenerator class OrderNumberGenerator(CoreOrderNumberGenerator): def order_number(self, basket=None): num = super().order_number(basket) return "SHOP-%s" % num
To obtain an Oscar app’s app config instance, look it up in the Django app registry.