Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-mptt TreeManyToManyField not working?

I try for a little while set up django-mptt in my project. I took a sample from tutorial and changed model acordingly which looks like this:

class Genre(MPTTModel):
pk = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, unique=True)
parent = TreeManyToManyField('self', null=True, blank=True, related_name='children')

class MPTTMeta:
    order_insertion_by = ['name']

Unfortunately console prints out something like this:

/srv/tokedu/local/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/models/base.pyc in _set_pk_val(self, value)
    426 
    427     def _set_pk_val(self, value):
--> 428         return setattr(self, self._meta.pk.attname, value)
    429 
    430     pk = property(_get_pk_val, _set_pk_val)
/srv/tokedu/local/lib/python2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/models/base.pyc in _set_pk_val(self, value)
    426 
    427     def _set_pk_val(self, value):
--> 428         return setattr(self, self._meta.pk.attname, value)
    429 
    430     pk = property(_get_pk_val, _set_pk_val)
RuntimeError: maximum recursion depth exceeded

I think django-mptt just not support TreeManyToManyField. Anyone had the same problem??

like image 747
delete Avatar asked Nov 20 '25 10:11

delete


1 Answers

Nodes can't have more than one parent. That fundamentally changes your data structure - it's no longer a tree, it's an arbitrary graph.

django-mptt only handles trees. If you have a large graph database, you've got a very different problem. You might want to look into using a graph database rather than an RDBMS.

AFAIK there's no django apps which make graph structures easy, but then again I've never really needed to :)

Useful links:

  • http://en.wikipedia.org/wiki/Graph_database
  • http://techportal.inviqa.com/2009/09/07/graphs-in-the-database-sql-meets-social-networks/
like image 191
craigds Avatar answered Nov 22 '25 01:11

craigds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!