Django MPTT-如何根据URL段进行查询 [英] Django MPTT - how to query based on URL segments
问题描述
我有:
Home
Things with Fur
Dog
Horse
Basket Ball Games
Twenty One
Horse
假设每个slug
为title.lower().replace(' ', '-')
,则我的网址为:
Assuming the slug
for each is title.lower().replace(' ', '-')
, my URLs would be:
/home/
/home/things-with-fur/
/home/things-with-fur/dog/
/home/things-with-fur/horse/
/home/basket-ball-games/
/home/basket-ball-games/twenty-one/
/home/basket-ball-games/horse/
slug
字段是unique_together
,而parent
.
我需要根据URL段,查询正确的类别.我不能简单地用Category.objects.get(mptt_level=2, slug=u'horse')
查询篮球比赛horse
,因为碰巧有一个弹头horse
属于不同的类别,但级别相同.因此,我应该如何正确查询我的horse
类别,而不必爬上链条并检查每个级别?
I need to, based on the URL segements, query for the correct category. I can't simply query for the basket ball game horse
with Category.objects.get(mptt_level=2, slug=u'horse')
, because there happens to be a category with the slug horse
under a different category, but at the same level. So, how am I supposed to query for my horse
category correctly without climbing up the chain and checking each level?
推荐答案
将完整路径存储到数据库中的每个项目.
Store the full path to each item in the database.
这篇关于Django MPTT-如何根据URL段进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!