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

373
Visualizações
Display foreign key info within Django Detail View Template

As the title says I have a detailed view that I'm presenting via Django templates. I have a foreign key that I'd also like to present within that detailed view and I just can't get it to work. I've tried everything, but all I get is the basic detailed view template with no foreign key info. Any help would be greatly appreciated.

Here's what I've got so far:

Models:

class Cust(models.Model): #this is the main model
    id = models.UUIDField(
        primary_key=True,
        default=uuid.uuid4,
        editable=False)
    email = models.CharField(max_length=200)
    firstName = models.CharField(max_length=200)
    lastName = models.CharField(max_length=200)
    watchmanSlug = models.CharField(max_length=200, unique=True)
class Watchman(models.Model):
    group = models.ForeignKey(Cust, on_delete=models.CASCADE,to_field='watchmanSlug', 
    related_name='watchman_group_slug')
    uid = models.CharField(max_length=500)
    computer_name = models.CharField(max_length=500)
    computer_url = models.CharField(max_length=500)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Views

class CustomerDetailView(DetailView):
    model = Cust
    template_name = 'cust/cust_detail.html'
    
    def get_context_data(self, ** kwargs):
        context = super(CustomerDetailView, self).get_context_data( ** kwargs)
        context['computer_name'] = Watchman.objects.all()
        
        return context

Detail Template


                     <tbody>
                        <ul>
                          {% for p in watchman_group_slug.all %}
                          <li>{{ watchman.computer_name }}</li>
                          {% endfor %}
                
                      </ul>
                      </tbody>

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You access the related Watchman objects with:

<tbody>
  <ul>
    {% for p in object.watchman_group_slug.all %}
      <li>{{ p.computer_name }}</li>
    {% endfor %}
  </ul>
</tbody>

so with object.watchman_group_slug.all, and with the .computer_name of p.

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