Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

530
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda