Innovate anywhere, anytime withruncode.io Your cloud-based dev studio.
Django

How to Customize Django Oscar Models, Views and URLs

2022-07-19

Steps to fork/customise an app:

1. If you are forking an Oscar app for the first time, then you have to create a root apps-folder in which all your forked apps will exists:

$ mkdir yourappsfolder
$ touch yourappsfolder/__init__.py

2. Create a python module with the same 'app-label' as the Oscar app:

Ex: Customising oscar.apps.catalogue app

$ mkdir yourappsfolder/catalogue
$ touch yourappsfolder/catalogue/__init__.py

3. If the Oscar app has a models.py, then you have to create a models.py file in your local app.

# your custom models go here
from oscar.apps.catalogue.models import *

NOTE: To customise Oscar’s models, you must add your custom one before importing Oscar’s models. Then, your models file will have two models with the same name within an app, Django will only use the first one.

Ex: To add a active field to the product model:

# yourappsfolder/catalogue/models.py

from django.db import models
from oscar.apps.catalogue.abstract_models import AbstractProduct

class Product(AbstractProduct):
    active = models.BooleanField(default=False)

from oscar.apps.catalogue.models import *

4. Create an 'admin.py' file in your local app.

# yourappsfolder/catalogue/admin.py
from oscar.apps.catalogue.admin import *

5. Then copy the 'migrations' directory from oscar/apps/catalogue and put it into your new local catalogue app.

6. Added it as Django app by replacing Oscar’s app with your own in INSTALLED_APPS.

# settings.py

from oscar import get_core_apps
INSTALLED_APPS = [
    ...,
    # all your non-Oscar apps
] + get_core_apps(['yourappsfolder.catalogue'])

NOTE: get_core_apps([]) will return a list of Oscar core apps or else if you give a list of your custom apps, they will replace the Oscar core apps.

7. Finally, create migrations using 'makemigrations' management command and apply the migrations by using 'migrate catalogue' management command. Then, you can see that a new column is been added to the product model.

Steps for customizing URL's:

1. Follow the same above steps described in customizing an app.

2. In Oscar, each app will have its own URLs in the 'app.py' file and each app comes with an application instance with 'urls' property, which used to access the list of URL's of an app.

* Modify 'yourproject/urls.py' file to include Oscar URL's.

from django.conf.urls import include, url
from yourproject.app import application

urlpatterns = [
   # Your other URLs
   url(r'', include(application.urls)),
]

3. To change the URL for the basket app from 'basket' to 'cart', you need to customize root app instance by creating a sub-class of it and overriding the 'get_urls' method.

yourproject/app.py

from oscar import app

class Shop(app.Shop):
    def get_urls(self):
        urlpatterns = [
            url(r'^cart/', include(self.basket_app.urls)),
            # ...
            # Remianing urls here
        ]
        return urlpatterns

application = Shop()

Steps for customizing or adding views to an app:

1. Follow the same above steps described in customizing an app.

2. Create a new view class or create a sub-class of Oscar's view in views.py file:

Ex: Add extra context to the home page.

from oscar.apps.promotions.views import HomeView as CoreHomeView

class HomeView(CoreHomeView):
    
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)

        context["latest_products"] = Product.objects.filter(
            parent=None).order_by('-date_created')

        return context