1. How to extend django-SHOP models

(Instead of the default ones)

Some people might feel like the django shop models are not suitable for their project, or want to extend functionality for their specific needs.

This is a rather advanced use case, and most developers should hopefully be happy with the default models. It is however relatively easy to do.

All models you can override have a corresponding setting, which should contain the class path to the model you wish to use in its stead.

Note

While your models will be used, they will still be “called” by their default django-SHOP name.

1.1. Example

Extending the Product model in django-SHOP works like this:

# In myproject.models
from shop.models.productmodel import Product
class MyProduct(BaseProduct):
    def extra_method(self):
        return 'Yay'

    class Meta:
        pass

# In your project's settings.py, add the following line:
SHOP_PRODUCT_MODEL = 'myproject.models.MyProduct'

Important

Your model replacement must define a Meta class. Otherwise, they will inherit their parent’s Meta, which will break things. The Meta class does not need to do anything important - it just has to be there.

From a django interactive shell, you should now be able to do:

>>> from shop.models.productmodel import Product
>>> p = Product.objects.all()[0] # I assume there is already at least one
>>> p.extra_method()
Yay
>>> p.__class__
<class object's class>

1.2. Settings

All available settings to control models overrides are defined in General Settings

Table Of Contents

Previous topic

6. How to secure your views

Next topic

2. How to use your own addressmodel

This Page