0

screen shot 2016-07-20 at 17 06 39

Environment: python 2.7.10, Django 1.9.1, django-mptt 0.8.4

# models.py
class Foo(MPTTModel):
    parent = TreeForeignKey('self', null=True, blank=True)

Error raises in:

getattr(self, opts.tree_id_attr) != getattr(parent, opts.tree_id_attr)

where parent is None because of:

1) link

opts.set_raw_field_value(self, opts.parent_attr, old_parent_id) # old_parent_id is None

2) link

parent = getattr(self, opts.parent_attr)

set_raw_field_value source:

def set_raw_field_value(self, instance, field_name, value):
    field = instance._meta.get_field(field_name)
    setattr(instance, field.attname, value)

Help me to understand this behavior. Why it's not enough to set relation by self.parent_id?

1 Answers1

0

It seems that setting parent_id does not reset parent in any way, resp. parent isn't dynamically loaded from the database just because the underlying id field has some value.

https://github.com/django-mptt/django-mptt/issues/428