how to use: from __future__ import division with django -
i'm trying use __future__ import division
django operation won't work in views.py.
under django shell great same python shell:
>>> __future__ import division >>> result = 37 / 3 >>> result 12.333333333333334 >>>
the same thing in django views.py don't works when try use it.
error message: unsupported operand type(s) /: 'int' , 'instancemethod'
views.py :
from __future__ import division def show_product(request, product_slug, template_name="product.html"): review_total_final = decimal.decimal('0.0') review_total = 0 product_count = product_reviews.count # number of product rated occurences if product_count == 0: return review_total_final else: product_torate in product_reviews: review_total += product_torate.rating review_total_final = review_total / product_count return review_total_final return review_total_final
models.py:
class productreview(models.model): ratings =((5,5),.....,) product = models.foreignkey(product) rating = models.positivesmallintegerfield(default=5, choices=ratings) content = models.textfield()
product_reviews queryset.
any !!!
from __future__ import division
has nothing this; you're trying divide value method itself, instead of calling method first proper operand. compare , contrast:
>>> class x(object): ... def count(self): ... return 1 ... >>> x = x() >>> 1 / x.count traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: unsupported operand type(s) /: 'int' , 'instancemethod' >>> 1 / x.count() 1
Comments
Post a Comment