4
Talk guys, I haven’t found a north or how to research on, so I’m going to go to the O.R..
I have the following in my models.py, Ingredient and Product:
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)
and
class Produto(models.Model):
nome = models.CharField(max_length=200)
custo_produto = #aqui minha dúvida
porcentagem_lucro = models.DecimalField(max_digits=10, decimal_places=2)
valor_venda = models.DecimalField(max_digits=10, decimal_places=2)
descricao = models.TextField()
ingredientes = models.ManyToManyField(Ingrediente)
I wonder how I can make that custo_produto
be a sum of custo
of the instances of Ingrediente
that are associated with the Produto
.
Do you understand? The sum of the cost of the ingredients is the cost of the product.
Thanks for your help, but I still had problems. I tested both solutions by copying the code to make sure it was not wrong, and I got an error: "'Manyrelatedmanager' Object is not iterable". Remember that in my template I used {{ products.custo_product ?
– Giovane Machado
the use between two keys is to use in a template - is another language programming - here we are talking about the Python part. Are you trying to see the cost_product of an instance (a specific product), or direct from the class? In the class will give error. Let me take a look at instances if you need something else. (Django is usually "pythonico", but sometimes they screw up)
– jsbueno
I understand jsbueno, the idea is to play this value for the Django template, so I thought of this syntax because it is the same as I use for the other attributes of the Product class. On the page specifically I pull the details of each instance, as its name, description, and now would also have its cost_product
– Giovane Machado
Ready - I found the problem - the Manytmany field is not eternal in itself - it is a "Queryset" of Django - need to call the method
.all()
in it so that it returns an iterable for all related objects. I changed the code to do this.– jsbueno
Inside the template you can also use direct now:
{{ produto.custo_produto }}
– jsbueno
I’ll test it right now!
– Giovane Machado
That was perfect boss, only two more things to say: 1 - no for, the correct is "ingredients" with "s", and not without the "s" (tested and confirmed hahaha). 2 - If you can suggest me where to learn more about this and how you came to this solution, thank you, I’m reading your other answer (which Oce linked, about properties). Thank you! Working!
– Giovane Machado
The answer I called about "Property" is a good start. The rest is programming practice even - the ideal is to have enough resourcefulness to arrive at the first solution without having any doubt. The second solution is more specific to Python.
– jsbueno
As for variable no
for
, the variable name is created on its ownfor
so it isingrediente
same - who writes thefor
which gives the name. - there, if instead of a Python command it was a phrase in English, the delay would bepara [cada] ingrediente em ingredientes:
(and notpara [cada] ingredientes em ingredientes
) Could bea
,x
,paralelepipedo
- would work the same way.– jsbueno