Django Category and Subcategory searches

后端 未结 3 497
闹比i
闹比i 2021-02-04 17:08

I\'m attempting to use a similar Category implementation to this one in the Django Wiki. I\'m wondering what the Django way of doing a search to pull all objects associated wit

3条回答
  •  深忆病人
    2021-02-04 17:40

    If you want to enforce strict categories and subcategories but also have the ability to perform fast searches with results like you describe, you may want to make a "tag" table where you don't actually allow users to tag items themselves, but rather as soon as you assign a category to an item you fill in the tag table for that item with all the parent categories up to the root node of the category tree.

    For example, if you have the following: alt text http://img509.yfrog.com/img509/9845/photoho.jpg

    The tag table would look something like:

       id   |   tag_name   |   tv_id
       1    |     "tv"     |     1
       2    |     "sd"     |     1    
       3    |     "crt"    |     1  
       4    |     "tv"     |     2  
       5    |     "HD"     |     2  
       6    |     "LCD"    |     2  
       7    |     "tv"     |     3  
       8    |     "HD"     |     3  
       9    |   "plasma"   |     3
    

    Now your queryset will look like items=Item.objects.filter(tag='TV')

提交回复
热议问题