Foreign key relationship on Jango

Asked

Viewed 845 times

0

I have two tables in the Client and Animal model, where animal has a foreign key for Client, I can list all the animals related to the client in the table, but customers who do not have animals cannot listlos, como conseguir listar os cliente sem animais com os que possuem animais na mesma tabela: segue abaixo meu código.

models

 class Cliente(models.Model):

    codigoCliente = models.AutoField(primary_key=True, blank=False)
    nome = models.CharField(max_length=50, null=False, blank=False)
    sobrenome = models.CharField(max_length=50, null=False, blank=False)
    rg = models.CharField(max_length=9, null=False, blank=False)
    cpf = models.CharField(max_length=11, null=False, blank=False)
    dataNascimentoCliente = models.DateField(null=True, blank=True, verbose_name='Data de Nascimento')
    numeroTelefoneFixo = models.CharField(max_length=11, null=True, blank=True, verbose_name='Telefone Fixo')
    numeroTelefoneCelular = models.CharField(max_length=11, null=True, blank=True, verbose_name='Telefone Celular')
    email = models.EmailField(max_length=100, unique=True, null=False, blank=False)
    cep = models.CharField(max_length=8, null=False, blank=False)
    endereco = models.CharField(max_length=100, null=False, blank=False)
    cidade = models.CharField(max_length=100, null=False, blank=False)
    bairro = models.CharField(max_length=100, null=False, blank=False)


    def __str__(self):
        return '{}, {}'.format(self.nome, self.sobrenome)

    def __repr__(self):
        return '{}, {}'.format(self.nome, self.sobrenome)


class Animal(models.Model):

    codigoAnimal = models.AutoField(primary_key=True, blank=False)
    nomeAnimal = models.CharField(max_length=50, null=False, verbose_name='Nome do Animal')
    dataNascimentoAnimal = models.DateField(null=True, blank=True)
    pelagem = models.CharField(max_length=10, choices=PELAGEMCHOICES)
    especie = models.CharField(max_length=100, null=True, blank=True)
    raca = models.CharField(max_length=50, null=False, verbose_name='Raça')
    sexo = models.CharField(max_length=10, choices=SEXOCHOICES)
    deficiencia = models.CharField(max_length=10, choices=DEFICIENCIACHOICES)
    descreva = models.TextField(null=True, blank=True)
    imagem = models.ImageField(upload_to='imagens', null=True, blank=True, verbose_name='Foto do Animal')

    cliente = models.ForeignKey(Cliente, on_delete=models.CASCADE)

    def __str__(self):
        return '{}, {}'.format(self.nomeAnimal, self.raca)

    def __repr__(self):
        return '{}, {}'.format(self.nomeAnimal, self.raca)

in the views

@login_required(login_url='/login/')
def consultaRegistro(request, template_name='consultaRegistro.html'):


    animal = Animal.objects.all()



    return render(request, template_name, {'animal': animal })

**template**

                    {% for a in animal  %}
                    <tr>

                      <td><a href="/consultaDetails/{{ c.cliente_id }}/">{{ a.cliente_id }}</a></td>
                      <td><a href="/consultaDetails/{{ c.cliente_id }}/">{{ a.cliente.nome }}</a></td>
                      <td><a href="/consultaDetails/{{ c.cliente_id }}/">{{ a.nomeAnimal }}</a></td>


                    </tr>
                    {% endfor %}
  • If you need all the customers, it wouldn’t be enough to do Cliente.objects.all()?

  • yes, I’ve done this only when I scan the template I end up having to do two FOR, so I can’t put the animal records the client records in the same table.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.