我在实施django mptt时遇到问题。
这是我的模特:
class Company(models.Model):
name = models.CharField( max_length=100)
parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
mptt.register(Company, order_insertion_by=['name'])
并且
class Financials(models.Model):
company = models.ForeignKey(Company, related_name="financials")
year = models.IntegerField()
revenue = models.DecimalField(max_digits = 10, decimal_places = 2)
所以我要看的是如何将金融作为一个孩子添加到公司。
我试过了mptt.register(Financials, parent = Company)
,这当然给了我错误。
所以mytree结构将是:
company1
....................> Financial1
--------------------> Financial 2
company2
-------------------->Financial 3
由于
答案 0 :(得分:1)
Django-mptt不支持同一树中的多种类型的对象。您可以让Financial和Company都从同一个父类继承,然后从该父类的实例构建树。您需要在父类上存储“内容类型”字段,以便可以将父类的实例强制转换为正确的子类。这是一个严重的黑客,因为它违反了继承的精神。财务不是公司,而是公司的属性。正确答案是修复您的ACL设计,以便您可以使用ForeignKey。
答案 1 :(得分:1)
我推荐django-polymorphic_tree