将 select 显示为树
Show select as a tree
如何使用 MPTT 在 Django 管理中将类别显示为树?
我让它以树的形式显示类别列表:
但是当我尝试创建新文章时它不起作用:
如何让它呈现 select 为一棵树?
这是我的模型:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from ckeditor.fields import RichTextField
from taggit.managers import TaggableManager
import os
class Category(MPTTModel):
name = models.CharField(u'Заголовок', max_length=100, unique=False)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True,
verbose_name=u'Родитель')
class Meta:
verbose_name = u'Мероприятие'
verbose_name_plural = u'Мероприятия'
class MPTTMeta:
order_insertion_by = ['name']
def __unicode__(self):
return self.name
class Article(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=False, verbose_name=u'Раздел')
title = models.CharField(u'Содержание', max_length=120, blank=False)
content = RichTextField(u'Содержание', blank=False)
tags = TaggableManager(u'Тэги')
class Meta:
verbose_name = u'Новость'
verbose_name_plural = u'Новости'
您应该在您的子模型中使用 TreeForeignKey
:
class Article(models.Model):
category = TreeForeignKey(Category, on_delete=models.CASCADE, blank=False,
null=False, verbose_name=u'Раздел')
...
如何使用 MPTT 在 Django 管理中将类别显示为树?
我让它以树的形式显示类别列表:
但是当我尝试创建新文章时它不起作用:
如何让它呈现 select 为一棵树?
这是我的模型:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
from ckeditor.fields import RichTextField
from taggit.managers import TaggableManager
import os
class Category(MPTTModel):
name = models.CharField(u'Заголовок', max_length=100, unique=False)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True,
verbose_name=u'Родитель')
class Meta:
verbose_name = u'Мероприятие'
verbose_name_plural = u'Мероприятия'
class MPTTMeta:
order_insertion_by = ['name']
def __unicode__(self):
return self.name
class Article(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=False, verbose_name=u'Раздел')
title = models.CharField(u'Содержание', max_length=120, blank=False)
content = RichTextField(u'Содержание', blank=False)
tags = TaggableManager(u'Тэги')
class Meta:
verbose_name = u'Новость'
verbose_name_plural = u'Новости'
您应该在您的子模型中使用 TreeForeignKey
:
class Article(models.Model):
category = TreeForeignKey(Category, on_delete=models.CASCADE, blank=False,
null=False, verbose_name=u'Раздел')
...