How to customize Django Oscar Models, Views and URL's

In order to customize models, views and urls, you need to fork an Oscar core app in which model/view resides. Then you should be able to override any model/view classes.

Steps to fork/customize 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
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Sending emails using sendgrid on heroku for a Django App Vinisha Naladala

Integrate Sendgrid API to your Heroku app to deliver simplified emails like any notification emails, user signups etc.

Continue Reading...
Integration of 2checkout with django Nikhila Mergu

Payment Gateways which facilitate communication within banks and Security is an integral component of all payment gateways, as sensitive data such as Credit Card Numbers ...

Continue Reading...
Extract data from PDF and all Microsoft Office files in python Ashwin Kumar

The quick way to get/extract text from PDFs in Python is with the Python library "slate". Slate is a Python package that simplifies the process ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties