简体   繁体   中英

how to have tags for tags

Here's what I'm trying to do. let's say I have a community page sports and I want to display it's related pages to "sports" page. I would like to show "football", "baseball", "basketball" to my "sports" page.

So I thought about using foreignkey relationship. but with what I've tried "football" page displays "sports". In admin, for football page, for the field, related_cat I'm able to choose other categories. I picked "sports" because I want "football" to be part of "sports". but it displays backward. football page shows sports.

class Category(models.Model): 
    name = models.CharField(max_length=128, unique=True)(Sponsored, null=True, blank=True)

    related_cat = models.ForeignKey("self", null=True, blank=True)




{% if category.related_cat %}

{{category.related_cat}}

{% endif %}

As an aside, Django has multiple tagging packages (see https://www.djangopackages.com/grids/g/tagging/ ).

However, for your question:

You might want to use a many to many instead of foreign key, but assuming you want a strict hierarchy, given an object Football which has a related_category of Sport , and another object Baseball which also has a related_category of Sport , you can get the list of all the things which have a related_category of Sport by doing Sport.category_set.all()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM