我已经阅读了一些相关的问题,但是对于我的案例来说,最好的做法不是很清楚。我是一个试图学习的数据库 n00b,所以耐心等待。我的情况如下:
- 3 种类型的公司:制造商、经销商、ServiceNRepair(共享大约 10 个属性)
- 5 种产品:StuffedAnimals、自行车、芭比娃娃(也共享一些属性)
所以为了保持干燥,我尝试了多表继承:
# 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 的典型解决方案是什么?我只是想知道下一步该研究什么来解决这个问题,这肯定很普遍。任何提示表示赞赏。谢谢你。