首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何扩展django-shop产品模型?

如何扩展django-shop产品模型?
EN

Stack Overflow用户
提问于 2014-10-08 07:09:20
回答 1查看 720关注 0票数 0

我正在尝试使用django-shop为一个在线商店创建一个产品模型。每个产品可能有许多颜色和大小,产品-颜色-大小组合可能在数量上不同,所以我创建了另一个颜色-大小-数量模型。

如何使我的GoodsDetail表中的数据作为Goods instance子类化Product的一部分?这样我就可以把整堆东西加到购物车里了。

下面是我的代码:

代码语言:javascript
复制
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"],
        ]
EN

回答 1

Stack Overflow用户

发布于 2017-04-05 17:15:53

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26246736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档