开发者

Django MPTT order_by error

开发者 https://www.devze.com 2023-02-25 22:51 出处:网络
models.py class Category(MPTTModel): name = models.CharField(max_length=100) slug = models.SlugField(max_length=200, unique=True)

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, 开发者_StackOverflow中文版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',]

My view:

categories = Category.tree.filter(level__lte=1)

I will order by "created" in the first level of the tree in my template - How do I do so? I tried:

categories = Category.tree.filter(level__lte=1).order_by('-created')

Please help...


You don't have parent

from mptt.models import MPTTModel, TreeForeignKey

parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

blog.v-live.pl - more django tips

0

精彩评论

暂无评论...
验证码 换一张
取 消