0
Hey, sorry for the confusing question (I don’t know exactly how to ask this), but come on. I’m wearing Django2.1.
I have these 2 models
related so ManyToMany
:
class Ingrediente(models.Model):
produtor = models.ForeignKey('auth.User', on_delete=models.CASCADE, default=10)
nome = models.CharField(max_length=200)
descricao = models.TextField()
quantidade = models.DecimalField(max_digits=10, decimal_places=3)
custo = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return self.nome
and
class Produto(models.Model):
nome = models.CharField(max_length=200)
descricao = models.TextField()
ingredientes = models.ManyToManyField(Ingrediente)
utilitarios = models.ManyToManyField(OutrosCustos)
def __str__(self):
return self.nome
Following use view
to return a list of all objects in the Product class:
def produtos_list(request):
produtos = Produto.objects.filter()
return render(request, 'egg_app/produtos_list.html', {'produtos':produtos})
Then the joke, in my template I return this way:
<table class="table">
<thead>
<tr>
<th scope="col">Nº Produto</th>
<th scope="col">Nome</th>
<th scope="col">Ingredientes</th>
<th scope="col">Custo</th>
</tr>
</thead>
<tbody>
{% for p in produtos %}
<tr>
<th scope="row">{{ p.pk }}</th>
<td>{{ p.nome }}</td>
<td>{{ p.ingredientes }}</td>
<td>custo teste</td>
</tr>
{% endfor %}
</tbody>
And my result has been:
I would like in the Ingredients column to have the names of the Ingredients and not that Ingredient.None.
Hey Rodrigo, thanks for the help, I’ve been away a few days so it took me a while to test. In the stack overflow international passed me this syntax {{ products.ingredientes.all }}, it returns the ingredients, but as query set. My challenge for now is to bring only the name attribute of the Ingredients class, in a list of Products ( this may be confusing, but if you look there the code I think you can understand).
– Giovane Machado
I checked the question you sent linked, and that’s not the case either
– Giovane Machado
update: I got it, I’ll do another answer here to show already. But thank you so much!
– Giovane Machado