如何扩展django-shop产品型号?

时间:2014-10-07 23:09:20

标签: django django-shop

我正在尝试使用django-shop为在线商店创建产品模型。每种产品可能有多种颜色和尺寸,产品颜色尺寸组合的数量可能不同,因此我创建了另一种颜色尺寸数量的模型。

如何从GoodsDetail表中创建数据作为Goods实例子类化Product的一部分?这样我就可以将整个货架添加到购物车中。

这是我的代码:

from shop.models import Product
from django.db import models

class Category(models.Model):  
#some code for Category

class Image(models.Model):
#some code for Image

class Size(models.Model):
#some code for Size

class Manufacturer(models.Model):
#some code for Manufacturer

class Goods(Product):
    #name = Product.name
    #unit_price = Product.unit_price
    category = models.ForeignKey(Category)
    serial_n = models.CharField(max_length=20)
    manufact = models.ForeignKey(Manufacturer, default=1, null=False, blank=True)
    short_decription = models.TextField(max_length=50, default='', blank=True)
    long_decription = models.TextField(max_length=250, default='', blank=True)

    class Meta:
        ordering = ['unit_price']

    def __unicode__(self):
        return self.name

class ProductAttribute(models.Model):
#some code for an optional attribute

class GoodsDetail(models.Model):
    goods = models.ForeignKey(Goods)
    COLOUR_CHOICES = (
        (u'blue', 'blue'),
        (u'green', 'green'),
    )
    colour = models.CharField(max_length=3, default='', choices=COLOUR_CHOICES)
    size = models.ForeignKey(Size, default='', blank = True)
    quantity = models.IntegerField(max_length=3, default=0)
    image = models.ForeignKey(Image, null=True, blank=True)
    attribute = models.ForeignKey('ProductAttribute', null=True, blank = True)

    class Meta:
        unique_together = [
        ["goods", "colour", "size"],
        ]

1 个答案:

答案 0 :(得分:0)

首先,Goods应继承自shop.models.product.BaseProduct,而不是Product

其次,您应该覆盖Goods中的ModelManager并使用shop.models.product.BaseProductManager或从中派生的类。