Arielis
Arielis

Reputation: 141

How to create products attributes like the one in django-shop

I liked the way how django-shop is creating new products attributes by developping a product model. Ex: SmartPhone... I would like to add products attributes the same way but I do not know by where to start. By experience, when I am copying code from an app, I end up deleting the app as it doesn't work correctly.

My product model is:

`class Product(models.Model):
     name = models.CharField('name', max_length=32)
     slug = models.SlugField('slug', max_length=32)
     description = models.TextField('description')

     class Meta:
          ordering = ['name']`

It would be great if you could advice me on how to add similar products attributes. This way I could create attributes like this example. I don't want to copy all the app because there is a lot of things that I don't need. [Smartcard example][1]https://github.com/awesto/django-shop/tree/master/example/myshop/models

Upvotes: 0

Views: 415

Answers (1)

jrief
jrief

Reputation: 1695

First of all, you have to decide, whether you need a polymorphic approach or not. I assume your products do not vary that much, hence you don't need polymorphism.

Therefore something such as the smartcard example should be enough:

from shop.money.fields import MoneyField
from shop.models.product import BaseProduct, BaseProductManager, CMSPageReferenceMixin
from shop.models.defaults.mapping import ProductPage, ProductImage

class Product(CMSPageReferenceMixin, BaseProduct):
    # common product fields
    product_name = models.CharField(max_length=32)

    slug = models.SlugField()

    unit_price = MoneyField(decimal_places=3)

    description = models.TextField("Description")

    objects = BaseProductManager()

Upvotes: 0

Related Questions