models.py
class Category(MPTTModel):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=200, unique=True)
parent = models.ForeignKey('self', blank=True, null=True, related_name='child')
image = models.ImageField(upload_to='gfx/category/image/', blank=True, null=True)
hidden = models.BooleanField(default=1)
created = models.DateTimeField(auto_now_add=True)
changed = models.DateTimeField(auto_now=True)
class MPTTMeta:
order_insertion_by = ['name',]
我的观点:
categories = Category.tree.filter(level__lte=1)
我将在模板的树的第一级“创建”命令 - 我该怎么做? 我试过了:
categories = Category.tree.filter(level__lte=1).order_by('-created')
请帮忙......
答案 0 :(得分:1)
你没有父母
from mptt.models import MPTTModel, TreeForeignKey
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')