PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 11, 2022

[FIXED] How does Django save decimal values?

 August 11, 2022     decimal, django, python     No comments   

Issue

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.


Solution

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.



Answered By - Yaroslav
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing