2

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)

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 an array of those ratings, their division gives an 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 the database, but rating does not.

Why? am I using wrong field type?

Alan.

Piotr Niewinski
  • 1,298
  • 2
  • 15
  • 27
Odif Yltsaeb
  • 5,575
  • 12
  • 49
  • 80

2 Answers2

2

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
  • 2,718
  • 17
  • 16
1

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
  • 2,204
  • 13
  • 15
  • You know. Seven years is one year in dog years... https://stackoverflow.com/a/44904020/343215 – xtian Mar 09 '21 at 20:34