1

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

Alex Bolotov
  • 8,781
  • 9
  • 53
  • 57
  • Do you need to add it as a leaf node? I thought you already solved that through the foreign key relation. – Staale Feb 03 '09 at 14:25
  • Agree with Staale; you have the ForeignKey, why does Financials need to be involved in the tree structure at all? – Carl Meyer Feb 03 '09 at 17:57
  • The problem here is I am trying to do some Acl stuff here. So for that I need to save model in mptt tree structure. So If I remove the foreignkey from financial then how can I add financail as an child to company?? –  Feb 04 '09 at 05:31

2 Answers2

1

I recommend django-polymorphic_tree

thanos
  • 723
  • 7
  • 9
1

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.

Carl Meyer
  • 122,012
  • 20
  • 106
  • 116