This is the original category model:
class Category(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
def __unicode__(self):
return self.name
class MPTTMeta:
order_insertion_by = ['name']
Then I needed to order the category, so I altered it like the following:
class Category(MPTTModel):
name = models.CharField(max_length=50, unique=True)
order = models.SmallIntegerField() <<<<<<<<<
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
def __unicode__(self):
return self.name
class MPTTMeta:
order_insertion_by = ['order'] <<<<<<<<<
And I changed the Django admin declaration from:
admin.site.register(Category, MPTTModelAdmin)
To:
class CategoryAdmin(MPTTModelAdmin):
list_display = ('name', 'order')
list_editable = ('order',)
admin.site.register(Category, CategoryAdmin)
Then everything fall apart after making a few edits from the admin control panel. I can't describe exactly what happened but it seems the lft
, rght
, level
and parent_id
where messed up by these changes.
Am I using order_insertion_by
in the wrong context? Is it for something else? I tried to search the docs but didn't get a useful answer.
I faced this problem. The problem is not in the package django-mptt, and in the framework Django, more precisely in the admin. Perhaps this is due to several administrators working simultaneously. While only one solution - to abandon the
list_editable
in admin class or write a script with the same fieldorder
for Ajax.In order to restore the tree, use
rebuld
method:Category.tree.rebuild()