1
I have two tables (Client) and (Animal), the animal table is related through a Foreign key with the Client table, currently I can register only client, register client and animal together, in the list screen I can list the data of the client table and data of the animal table including the clients that are related to the animal, however I would like to list now only the clients that do not have the registered animal: follows below my models.
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)
views
@login_required(login_url='/login/')
def consultaRegistro(request, template_name='consultaRegistro.html'):
animal = Animal.objects.all()
cliente = Cliente.objects.all()
return render(request, template_name, {'cliente': cliente, 'animal': animal }
template****
<tr>
<th>ID Cliente</th>
<th>Cliente</th>
<th>Animal</th>
</tr>
{% for a in animal %}
<tr>
<td><a href="/consultaDetails/{{ xxxxxxxxx }}/">{{ a.cliente.codigoCliente }}</a></td>
<td><a href="/consultaDetails/{{ xxxxxxxxx }}/">{{ a.cliente.nome }}</a></td>
<td><a href="/consultaDetails/{{ xxxxxxxxx }}/">{{ a.nomeAnimal }}</a></td>
</tr>
{% endfor %}
{% for c in cliente %}
<tr>
<td><a href="/consultaDetails/{{ xxxxxxxxxx }}/">{{ c.codigoCliente }}</a></td>
<td><a href="/consultaDetails/{{ xxxxxxxxx }}/">{{ c.nome }}</a></td>
<td><a href="/consultaDetails/{{ xxxxxxxxx }}/">{{ a.nomeAnimal }}</a></td>
</tr>
{% endfor %}
Do you want a query that returns or how will it display in the template? It is not clear what your question is. What have you done? where is the error? Place the current query.
– Ricardo
Good evening Ricardo edited the post, put the views and the template. i want to be a query where I can list only clients who do not currently have animals in the views I am listing all clients.
– Edson
Thank you Ricardo worked perfectly!!
– Edson