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_bases import BaseProduct
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, it will inherit its parent’s Meta
, which will break
things. The Meta
class does not need to do anything important - it
just has to be there.
Note
The above example is intentionally not using the same module as
the examples given earlier in this documentation (myproject versus
myshop). If MyProduct and Book were defined in the same module a circular
import will result culminating in an error similar to:
django.core.exceptions.ImproperlyConfigured: Error importing backend
myshop.models: "cannot import name Product". Check your SHOP_PRODUCT_MODEL
setting
.
From a django interactive shell, you should now be able to do:
>>> from shop.models 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 model overrides are defined in General Settings