Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:17:13+00:00 2026-06-11T21:17:13+00:00

I have a Django model with a decimal field: class User(models.Model): … balance =

  • 0

I have a Django model with a decimal field:

class User(models.Model):
    ...
    balance = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
    ...

I have my app running on my development machine (Python 2.7.1, Mac OSX, Django 1.4, MySQL InnoDB), and on a production server (Python 2.6.5, Ubuntu, Django 1.4, MySQL ISAM)

The problem is on my development machine. This code works without throwing an error:

>>> u = User.objects.get(pk=1)
>>> u.balance
Decimal('9.00')
>>> u.balance=1.44
>>> u.save()
>>> u.balance
1.44

As far as I understand, u.save() should through the following error, which it does on my production instance:

>>> u.save()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 463, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 529, in save_base
    rows = manager.using(using).filter(pk=pk_val)._update(values)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 557, in _update
    return query.get_compiler(self.db).execute_sql(None)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 986, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 808, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 951, in as_sql
    val = field.get_db_prep_save(val, connection=self.connection)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py", line 873, in get_db_prep_save
    return connection.ops.value_to_db_decimal(self.to_python(value),
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/__init__.py", line 847, in to_python
    return decimal.Decimal(value)
  File "/usr/lib/python2.6/decimal.py", line 649, in __new__
    "First convert the float to a string")
TypeError: Cannot convert float to Decimal.  First convert the float to a string

So why is my development instance not throwing this error? Problem came up because I checked in some buggy code that passed my unit tests. This error should have been thrown. I put my environment details because it has to be due to one of those differences. I suspect MySQL ISAM vs. InnoDB, but not at all sure.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:17:14+00:00Added an answer on June 11, 2026 at 9:17 pm

    Your dev environment running Python 2.7 should not throw that error, but your production environment running Python 2.6 should.

    From the Python docs on Decimal objects:

    Changed in version 2.7: The argument to the constructor is now permitted to be a float instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic Django model like: class Business(models.Model): name = models.CharField(max_length=200, unique=True) email
I have a Django model with a field for price: class Book(models.Model): title =
I have django model: class UserProfile(models.Model): user = models.OneToOneField(User) #other fields When I create
I have a django model, which has a int field (with null=True, blank=True). Now
I have a Django model User with field balance , and maintain a balance_last_updated
If I have a Django model with a FileField e.g., class Probe(models.Model): name =
I have a model which looks like this: class MyModel(models.Model) value = models.DecimalField() date
I have Django model that looks like this: class Categories(models.Model): Model for storing the
So I have a few Django models that look like this: class City(models.Model): name
Let's say I have a set of Django Models: class Article(models.Model): title = models.CharField(max_length=100,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.