Django MPTT order_by 错误
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')
请帮忙...
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',]
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有父级
parent = TreeForeignKey('self', null=True, Blank=True, related_name='children')
blog.v-live.pl - 更多 django 技巧
You don't have parent
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
blog.v-live.pl - more django tips