产品、警报、预定义搜索中使用的模型(产品)属性……是否符合 DRY 概念?

发布于 2024-12-03 04:41:30 字数 1422 浏览 2 评论 0原文

首先我想说我不是一个经验丰富的程序员。

假设有一个模型产品,并且有一些具有不同属性的产品类型(例如,两种类型)。

我有一个 BaseProduct 模型,其中包含 User、creation_date、creation_ip 和其他一些属性,以及为此类产品属性指定的子类子模型。

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)

有一个选项可以根据某些条件创建新产品创建电子邮件警报,并且还有一个基于某些条件的“快速预定义搜索”。对于这些,我(再次......)创建了模型。

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)

这是正确的解决方案还是我应该为警报、预定义搜索创建一些通用属性模型??..我在这里多次重复属性字段。您对此有何看法?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-12-10 04:41:30

我将创建一个 AttributesBase 抽象模型并使用多重继承:

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

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


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

希望对您有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文