Display the value of a Manytomany Django object in the template?

Asked

Viewed 510 times

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:

inserir a descrição da imagem aqui

I would like in the Ingredients column to have the names of the Ingredients and not that Ingredient.None.

2 answers

1

  • 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).

  • I checked the question you sent linked, and that’s not the case either

  • update: I got it, I’ll do another answer here to show already. But thank you so much!

1


Come on, I got a solution.

First of all, the screening I was trying wasn’t pleasant, so I changed things. Instead of releasing everything in this table, I made a link in the name of each product, and launched it to a page of individual details. And there I made a list of the following logic:

To return a list with Queryset of all objects, I need this (which I was taught here:

 produtos.ingredientes.all

But unfortunately it returns a pure Queryset, something like:

<QuerySet [<Ingrediente: Ingrediente Teste 1>, <Ingrediente: Ingrediente Teste 2>]>

Still in the same comment, the friend suggested me a go, and then I did and that way I got the expected result:

{% for ingredientes_desse_produto in produtos.ingredientes.all %}
    <div class="col">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">{{ ingredientes_desse_produto.nome }}</h5>
                <p class="card-text">{{ ingredientes_desse_produto.custo }}</p>
            </div>
        </div>
    </div>
    {% endfor %}

I go through all the results with the first for, and use the individual attributes of the object just below.

Browser other questions tagged

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