Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

523
Views
Django - Get values from ManyToManyField which is foreing key

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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()
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!