Django MPTT-如何根据URL段进行查询 [英] Django MPTT - how to query based on URL segments

查看:120
本文介绍了Django MPTT-如何根据URL段进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

Home
    Things with Fur
        Dog
        Horse
    Basket Ball Games
        Twenty One
        Horse

假设每个slugtitle.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆