drabo2005
drabo2005

Reputation: 1096

how to use: from __future__ import division with django

i'm trying to use __future__ import division with django for my operation but it won't work in my views.py.

under django shell it great same with python shell:

>>> from __future__ import division
>>> result = 37 / 3
>>> result
12.333333333333334
>>> 

The same thing in django views.py don't works when i try to use it.

error message: unsupported operand type(s) for /: 'int' and '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 # the number of product rated occurences
    if product_count == 0:
        return review_total_final
    else:
        for 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 is a queryset.

Any help !!!

Upvotes: 1

Views: 736

Answers (1)

Fredrik
Fredrik

Reputation: 940

from __future__ import division has nothing to do this with this; you're trying to divide a value by the method itself, instead of calling the method first to get a proper operand. Compare and 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) for /: 'int' and 'instancemethod'
>>> 1 / x.count()
1

Upvotes: 2

Related Questions