Thursday, May 30, 2013

Queryset math

I think this is a newbie question, but I can't find the answer in the docs or Group posts.
 
If I have a value stored in my data tables like:
   a = Some_table.objects.filter(pk=10).values('number')
   b = Another_table.objects.filter(pk=2).values('another_number')
 
and maybe even a value from a form
    c = form...
    c.third_number
 
So, in my view.py, I'd like to be able to do something like
 
def....():
   a = Some_table.objects.filter(pk=10).values('number')
   b = Another_table.objects.filter(pk=2).values('another_number')
   c = form...c.third_number
   x = b - (a*c)
      return render(request, 'results.html', {'x':x})
 
I know, in a view.py, I can do something like a=123, b=456, c=789, x=b-(a*c) , render x....but as soon as those values come from the queryset I can't do math on them.  Is it because they are returned as u'value'?  So do I need to cast them as numbers?  I think when I tried that the error said I couldn't cast the queryset value as an int or float or..
 
Trying to following mvt(mvc) conventions and keep that stuff out of the template/html/javascript
 
I'm sure I'm either missing something simple or doing something dumb.  Any help?
 

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment