使用django mptt的问题 [英] Problem using django mptt

查看:332
本文介绍了使用django mptt的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行django mptt时遇到问题。



这是我的模型:

 code> class Company(models.Model):
name = models.CharField(max_length = 100)
parent = models.ForeignKey('self',null = True,blank = True, ='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)这当然会给我错误。



  company1 
.............. ......> Financial1
-------------------->财务2

company2
-------------------->财务3

谢谢

解决方案

Django-mptt不支持多种类型的对象在同一棵树中。您可以使Financial和Company都从同一个父类继承,然后从该父类的实例生成树。您需要在父类上存储内容类型字段,以便您可以将父类的实例转换为正确的子类。这是一个粗暴的黑客,因为它违反了继承的精神。财务不是公司,这是公司的属性。正确的答案是修复您的ACL设计,以便您可以使用ForeignKey。


I am having problem implementing django mptt.

Here is my model:

   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'])

And

class Financials(models.Model):
    company = models.ForeignKey(Company, related_name="financials")
    year = models.IntegerField()
    revenue = models.DecimalField(max_digits = 10, decimal_places = 2)

So what I am looking at is how to add Financial as an child to Company. I tried mptt.register(Financials, parent = Company) which of course give me error.

so mytree structure will be:

company1
....................> Financial1
--------------------> Financial 2

company2
-------------------->Financial 3

Thanks

解决方案

Django-mptt does not support multiple types of object in the same tree. You could have Financial and Company both inherit from the same parent class, and then build the tree out of instances of that parent class. You'd need to store a "content type" field on the parent class so you can cast instances of the parent class to the proper subclass. This is a gross hack though, as it violates the spirit of inheritance. A Financial is not a Company, it's an attribute of a Company. The correct answer is to fix your ACL design so you can use a ForeignKey.

这篇关于使用django mptt的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆