2012-05-04 37 views
1

我如何查詢:Django的MPTT - 基於URL段

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

假設slug每個爲title.lower().replace(' ', '-'),我的網址是:

/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/ 

slugunique_togetherparent

我需要根據URL的區段查詢正確的類別。我不能簡單地查詢籃球比賽horseCategory.objects.get(mptt_level=2, slug=u'horse'),因爲碰巧有一個類別與不同類別下的slu 012 horse,但在同一級別。那麼,我該如何正確查詢我的horse類別,而無需攀登鏈條並檢查每個關卡?

回答

2

存儲數據庫中每個項目的完整路徑。

+0

我很害怕:(但是我發現存儲一個簡單的完整路徑sha1會使它對於較大的站點來說更簡潔/更小的DB索引,謝謝Daniel。 – orokusaki