views:

125

answers:

2

Hello

what am i doing wrong?

class MyModel(models.Model):
  name = models.CharField(max_length=100, verbose_name=_("Name"), blank=False, null=False)
  rating = models.DecimalField(max_digits=2, decimal_places=2, default=0)

id i do something like that:

average = count/len(votes) 
mymodel.rating = average
mymodel.save()

count is the sum of all ratings of given MyModel and votes is array of those ratings, their division gives average rating for MyModel (sorry if i described it incorrectly due to my poor english)

if i change previous to:
mymodel.name = 'anything'
mymodel.save()

then 'anything' ends up in database, but rating does not.

why? am i using wrong field type?

Alan.

A: 

Models do no validation. The types you use pretty much deal with things like syncdb etc. rather than actual formatting of data, that's what a Form is for.

Lee
+1  A: 

max_digits is a number of digits before and after the decimal point. With your settings it allows you to store numbers between 0.00 and 0.99. Is it right? I can not figure out the range of values for average variable.

Yaroslav
Yea, if ratings can be between 0 and 5, for example, then `max_digits` needs to be 3.
Mike DeSimone
Ok, that could be it. I will test this asap.
Zayatzz
That seemed to be it :). Thanks.
Zayatzz