1👍
The point here is that Django automatically creates reverse lookup when you create a ForeignKey or ManytoManyField. Assuming your models are as follows:
BlogPost Model
from django.db import models
class BlogPost(models.Model):
title = models.CharField(_('title'), max_length=200)
slug = models.SlugField(_('slug'), unique_for_date='publish')
author = models.ForeignKey(User, blank=True, null=True)
body = models.TextField(_('body'), )
publish = models.DateTimeField(_('publish'), default=datetime.datetime.now)
created = models.DateTimeField(_('created'), auto_now_add=True)
Tag Model
from django.db import models
from Blog.models import BlogPost
class Tag(models.Model):
Post = models.ForeignKey(BlogPost,related_name="tags")
Now, assuming you are generating the Tags of a post in a view, you can basically get all the tags of a post by just calling blogpost.tags_set
where blogpost is a model instance of BlogPost
.
Source:stackexchange.com