Django MPTT - how to query based on URL segments

狂风中的少年 提交于 2020-01-05 11:49:28

问题


I have:

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

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/

The slug field is unique_together with the parent.

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?


回答1:


Store the full path to each item in the database.



来源:https://stackoverflow.com/questions/10455161/django-mptt-how-to-query-based-on-url-segments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!