How can I multiply 2 or more fields in django views or models

201 views
Skip to first unread message

jose angel encinas ramos

unread,
Jan 4, 2021, 4:00:01 PM1/4/21
to Django users
Hi everyone, i'm a new in django and python and i have a problem

I had a inventory app and I want to multiply 2 fields, coust_buy and quantity, but when execute this query:
views.py
        data = Articles.objects.all().annotate(result=F('coust_buy') * F('quantity'))

the results is this (img down)



what wrong with my query ? b

Screen Shot 2021-01-04 at 1.58.00 PM.png

Ayser shuhaib

unread,
Jan 4, 2021, 4:07:00 PM1/4/21
to django...@googlegroups.com
Nothing wrong with the query, the problem is that you are trying to multiply two values of a different type.
You can fix that by converting the smallInteger value to decimal.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f7e5dcbe-f6dd-47a1-8412-42c14aa70e95n%40googlegroups.com.

jose angel encinas ramos

unread,
Jan 4, 2021, 4:24:31 PM1/4/21
to django...@googlegroups.com
Ok, i understood, can you show me how to multiply 2 fields?
really i can't to do that... i guess i in mental looping

i try to do that in views.py:
data = Articles.objects.all().annotate(result=F('coust_buy') * F('quantity')).output_field=FloatField('result')

and the result is this.... what to you think 😬



--
José Ángel Encinas
Ing. Tecnologias de la información


Cel:  6622267620 

  
                   Never give up...
   I     I  https://us04web.zoom.us/j/4514417813
Screen Shot 2021-01-04 at 2.21.29 PM.png
Reply all
Reply to author
Forward
0 new messages