我已经阅读了一些相关的问题,但是目前尚不清楚最好的做法是我的情况。我是一个试图学习的数据库N00B,因此对耐心表示赞赏。我的情况如下:
- 3种公司类型:制造商,经销商,Servicenrepair(共享10个属性)
- 5种产品类型:毛绒玩具,自行车,芭比娃娃(也共享某些属性)
因此,要保持干燥,我尝试了多表继承:
# company.models.py
class GenericCompany(models.Model):
name, description, address, etc
class Manufacturer(GenericCompany): #can manufacture different things
stuff_specific_to_manufacturers
product = models.ManyToMany(GenericProduct)
class Reseller(GenericCompany): #can sell different things
stuff_specific_to_manufacturers
product = models.ManyToMany(GenericProduct)
etc for ServiceNRepair
和类似的产品,
# product.models.py
class GenericProduct(models.Model):
name, price, color, etc
class StuffedAnimal(GenericProduct):
fluffyness, ears_or_not, etc
class Bicycle(GenericProduct):
wheel_diameter, weight, etc
etc for Barbies
现在我需要执行
之类的查询- 显示所有红色的产品(这很容易)
- 该制造商生产哪些产品?
- 找到所有经销商X出售的自行车
但是我可以用M2M做吗?Manufacturer.objects.filter(product_icontains ='something')
这样的事情将行不通。所以,我完全走错了路吗?使用ContentTypes的典型解决方案是吗?我只想在解决这个问题的旁边要研究的方向一些方向,这肯定是很普遍的。任何提示都赞赏。谢谢。
您可以做诸如Manufacturer.objects.filter(product__name__icontains='something')
之类的事情,只需在产品中添加字段的名称。