1

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 !!!

karthikr
  • 97,368
  • 26
  • 197
  • 188
drabo2005
  • 1,076
  • 1
  • 10
  • 16
  • 1
    can you show the usage in your views.py ? – karthikr Jul 21 '13 at 19:38
  • views.py 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 #review_total_final= Ratestar.objects.filter(rate__gte = F(review_total) / F(product_count)) return review_total_final return review_total_final – drabo2005 Jul 21 '13 at 19:47
  • There's, um, a lot wrong. .count is probably supposed to be .count(), maybe same with .rating and .rating(), but the indentation is really funky, can't really tell. – AdamKG Jul 21 '13 at 19:51
  • it's count not count() , try it before saying lot wrong. ok – drabo2005 Jul 21 '13 at 19:53
  • Could you please show your product_review model? What is the type of `rating` field? – alecxe Jul 21 '13 at 19:59
  • i want this AdamKG to answer now if he really know django – drabo2005 Jul 21 '13 at 20:15
  • Obviously, count is a method and you should call it: `product_count = product_reviews.count()`. – alecxe Jul 21 '13 at 20:23
  • i understand people filling about this count() but nothing wrong with it. product_count = product_reviews.count when i format the {{product_count}} on the template i get the exact value. but other thing with count(). the matter is not there. – drabo2005 Jul 21 '13 at 20:27
  • No, this is the problem -- Django's template system will call the method if you pass in the method object instead of calling it, but Python won't. – Fredrik Jul 21 '13 at 22:42
  • ok, i will try to figure out , thanks Fredrik – drabo2005 Jul 21 '13 at 22:49
  • @ali2005 Wow. I try not to be an asshole, but it just so happens that function-call parens in templates vs python code [is my highest-rated answer evar](http://stackoverflow.com/a/422153/16361)... – AdamKG Jul 21 '13 at 23:27
  • avoiding violent answer is better and respectfull – drabo2005 Jul 21 '13 at 23:41
  • So I'm not sure what you're saying here, Adam -- was I supposed to know about a 4-year old answer so I could link to it? I just signed up... – Fredrik Jul 22 '13 at 17:21

1 Answers1

2

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
Fredrik
  • 940
  • 4
  • 10