django-mptt and change model instance parent

325 views
Skip to first unread message

Дмитрий Чернышов

unread,
Apr 7, 2014, 6:35:17 AM4/7/14
to django-...@googlegroups.com

I have a little problem with django-mptt 0.6.0. I have model MyModel with standard parent field and register as mptt-model through mptt.register(MyModel). I create MyModel instance obj_1 with None parent as 0 level object. After this i create MyModel instance obj_2 with None parent also as 0 level object. I try to change obj_2 parent value from None to obj_1. When I call save method of obj_2, mptt raise

AttributeError: 'NoneType' object has no attribute 'tree_id'.

Problem at mptt/models.py:782 line, where we try do this:

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

This error raised, because before this we have 2 step:

  1. opts.set_raw_field_value(self, opts.parent_attr, old_parent_id) - where old_parent_id is None
  2. parent = getattr(self, opts.parent_attr) - where parent, of course, also is None

Is that bug?

Craig de Stigter

unread,
Apr 13, 2014, 4:39:58 PM4/13/14
to django-...@googlegroups.com
I see this was a week ago, did you get any further with it?

It works for me with this code:

In [16]: o1 = Category()
In [17]: o2 = Category()
In [18]: o1.save()
In [19]: o2.parent = o1
In [20]: o2.save()

If you didn't get it working could you paste your code and your model? And probably a full traceback too.

Thanks, sorry it took me a week to get this far.


--
You received this message because you are subscribed to the Google Groups "django-mptt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-mptt-d...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arthur Mullakhmetov

unread,
Jul 21, 2016, 2:08:17 AM7/21/16
to django-mptt-dev
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
Reply all
Reply to author
Forward
0 new messages