Dear all,
for testing purposes I set up a simple MPTT model following the
documentation. Then I created a couple of instances using the admin,
and created a method for randomly generating N instances in the tree:
=====================================
from django.db import models
from mptt.models import MPTTModel
class Concept(MPTTModel):
name = models.CharField(max_length=30)
parent = models.ForeignKey('self', null=True, blank=True,
related_name='children')
class MPTTMeta:
order_insertion_by=['name']
def __unicode__(self):
return
self.name
import random
def generate_subclasses():
all_choices = list(Concept.objects.all())
for x in range(10):
current = random.choice(all_choices)
nname = str(x) + str(random.random())
new = Concept(name= nname)
new.save()
new.move_to(current)
=====================================
But each time I run this I get an error. Tried various alternative
options, but still I can't understand where the error is.... any
ideas?
*****************************************************
>>> generate_subclasses()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/code/django/projects/ideas_and_thoughts/src/ideas/../
ideas/mptt_test/models.py", line 41, in generate_subclasses
new.move_to(current)
File "/Users/code/django/projects/ideas_and_thoughts/src/ideas/../
ideas/mptt/models.py", line 401, in move_to
self._tree_manager.move_node(self, target, position)
File "/Users/code/django/projects/ideas_and_thoughts/src/ideas/mptt/
managers.py", line 257, in move_node
self._move_root_node(node, target, position)
File "/Users/code/django/projects/ideas_and_thoughts/src/ideas/mptt/
managers.py", line 789, in _move_root_node
raise InvalidMove(_('A node may not be made a child of any of its
descendants.'))
InvalidMove: A node may not be made a child of any of its descendants.
*****************************************************
Thanks in advance,
Mike
p.s.
I'm using MPTT 0.4.2 and django 1.1