产品、警报、预定义搜索中使用的模型(产品)属性……是否符合 DRY 概念?
首先我想说我不是一个经验丰富的程序员。
假设有一个模型产品,并且有一些具有不同属性的产品类型(例如,两种类型)。
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将创建一个 AttributesBase 抽象模型并使用多重继承:
希望对您有所帮助。
I would make an AttributesBase abstract model and use multiple inheritance:
Hope that helps you out.