please help to understand.
I have the next models :
class TagsList(models.Model):
tags_list = models.CharField(max_length=30)
def __str__(self):
return self.tags_list
class Blog(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(TagsList)
how i can get related tags by object (in my case object with post_id)?
It's my view file :
def single(request, post_id):
object_post = Blog.objects.get(id=post_id)
tags = TagsList.objects.all()
content = {
'object_post': object_post,
'tags': tags,
}
return render(request, 'single.html', content)
I tried all cases, but how to include to content exactly tags which are related to this object, don't know. Thanks all, for the help.
P.S. Using django 1.11
initial answer from comments:
In a many-to-many relation you can access the related objects, try this in def single:
tags=object_post.tags.all()