8. The Polymorphic Product Model

The demo provided by cookiecutter-django-shop using the product model “polymorphic”, shows how to setup a shop, with different product types. This is where polymorphism enters the scene. In our example we use a combination from the simpler demos “commodity” and “smartcard”.

Since in this example, we have to specialize our product out of a common base, the properties for each product type are shared accross two models. In our demo, the base model is declared by the class myshop.models.Product. Here we store the properties common to all product types, such as the product’s name, a caption, etc.

The model classes for Smart Card, Smart Phone and a variation of Commodity then inherits from this base product class. These models additionally declare their model fields, which are required to describe the physical properties of each product type. Since they vary, we also have to create special templates for the detail views of each of them. Smart Phones for instance allow product variations, therefore we must adopt the template for adding the product to the cart.

8.1. The Internationalized Polymorphic Product Model

The i18n_polymorphic demo is a variation of the above example, with a few attributes translated into multiple languages, namely caption and description. This sample implementation does not use translated slugs, although it would be possible.

8.2. Next Chapter

In the next chapter of this tutorial, we will see how to organize the Cart and Checkout