Django-mptt管理员类别

时间:2017-06-11 14:16:20

标签: python django mptt

在我的Django项目中,我有一个模型:

class Category(MPTTModel):
name = models.CharField(default='',
                        max_length=50,
                        verbose_name='Название')
slug = models.SlugField(default='')
parent = TreeForeignKey('self',
                        related_name='children',
                        null=True,
                        blank=True,
                        verbose_name='Родительская категория'
                        )
order = models.PositiveSmallIntegerField(blank=False,
                                         null=False,
                                         default=0,
                                         verbose_name='Порядок')
is_active = models.BooleanField(default=True,
                                db_index=True,
                                verbose_name='Отображать на сайте')

class Meta:
    verbose_name = 'Категория'
    verbose_name_plural = 'категории'

class MPTTMeta:
    order_insertion_by = ['order']

如果我首先添加主要类别(一,二,三),然后添加子类别(四合一,五合二,六分之三),我希望在管理面板中看到它如下: / p>

-one
--four
-two
--five
-three
--six

但我有这个订单:

-one
-two
-three
--four
--five
--six

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您需要使用Category注册MPTTModelAdmin模型 在您的admin.py

from django.contrib import admin
from mptt.admin import MPTTModelAdmin
from .models import Category

admin.site.register(Category, MPTTModelAdmin)

参考:https://django-mptt.github.io/django-mptt/admin.html

答案 1 :(得分:0)

感谢名单!似乎来自suit.admin的SortableModelAdmin打破了订单。

我的admin.py是:

from suit.admin import SortableModelAdmin
from mptt.admin import MPTTModelAdmin
from .models import Category, Good


class CategoryAdmin(MPTTModelAdmin, SortableModelAdmin):
    mptt_level_indent = 20
    list_display = ('name', 'slug', 'is_active', 'order')
    list_editable = ('is_active',)
    prepopulated_fields = {"slug": ("name",)}
    # Specify name of sortable property
    sortable = 'order'

admin.site.register(Category, CategoryAdmin)