开发者

Model (Product) attributes used in product, alert, predefined search.. is it right with DRY concept?

First of all I would like to mention that I am not an experienced programmer.

Say there's a model Product, and there are some types of products with different attributes (well, 2 types for example).

I have one BaseProduct model with User, creation_date, creation_ip and some more attributes and subclassed child models with specified for this type of product attributes.

Class BaseProduct(models.Model):
    name = models.CharField()
    type = models.CharField()
    creation_date = models.DateField()
    creation_ip = models.IpAddressfield()

class ProductTypeX(BaseProduct):
    attribute_1 = models.ForeignKey(some_other_model)
    attribute_2 = models.ForeignKey(some_other_model2)

class ProductTypeY(BaseProduct):
    attribute_1 = models.ForeignKey(some_other_model)
    attribute_2 = models.ForeignKey(some_other_model2)

There's an option creating email alert on creation of new product based on some criteria, and there's also a "quick predefined search" based on some criteria too. For these I've created (again...) models.

class BaseAlert(models.Model):
    name = models.CharField()
    email = models.BooleanField()
    sms = models.BooleanField()

ProductTypeXAlert(BaseAlert):
    attribute_1 = models.ForeignKey(some_other_model)
    attribute_2 = models.ForeignKey(some_other_model2)

ProductTypeYAlert(BaseAlert):
    attribute_1 = models.ForeignKey(some_other_model1)
    attribute_2 = models.ForeignKey(some_other_model2)

class BasePredefinedSearch(models.Model):
    name = models.CharField()

ProductTypeXPredefinedSearch(BasePredefinedSearch):开发者_运维知识库
    attribute_1 = models.ForeignKey(some_other_model1)

Is this the right solution or should I create some universal Attribute model for alert, predefinedsearch ??.. I am repeating attributes fields here many times. What is your opinion on that ?

Thanks


I would make an AttributesBase abstract model and use multiple inheritance:

class AttributesBase(models.Model):
    class Meta:
        abstract = True

    attribute1 = models.ForeignKey(SomeOtherModel1)
    attribute2 = models.ForeignKey(SomeOtherModel2)


class ProductTypeX(BaseProduct, AttributesBase):
    """Defines product type X"""

Hope that helps you out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜