TypeError: __init__() got an unexpected keyword argument 'decimal_places'

Question:

I have this model defined in my models.py:

class FarmAnalytics(models.Model):
    water_saved = models.FloatField(default=0, decimal_places=2)
    last_full_execution = models.DateField(auto_now=False, auto_now_add=False)
    current_state = models.CharField(max_length=50) #watering/calculating/resting

And, I got the error below:

    Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Library/Python/2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Library/Python/2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Library/Python/2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/krishna/Documents/temp/tutorial/quickstart/models.py", line 10, in <module>
    class FarmAnalytics(models.Model):
  File "/Users/krishna/Documents/temp/tutorial/quickstart/models.py", line 11, in FarmAnalytics
    water_saved = models.FloatField(default = 0, decimal_places = 2)
TypeError: __init__() got an unexpected keyword argument 'decimal_places'

I couldn’t figure out what I was doing wrong, I’m fairly new to django, just started using it today.

Asked By: jack sexton

||

Answers:

The field type FloatField does not accept decimal_places as an option, it’s a option for DecimalField. You could try changing your FloatField by a DecimalField if it fit your needs.

Answered By: Gocht

I got the similar error below:

TypeError: init() got an unexpected keyword argument ‘max_digits’

Because I set max_digits=5 to BigIntegerField(), IntegerField(), SmallIntegerField(), PositiveBigIntegerField(), PositiveIntegerField() and PositiveSmallIntegerField() as shown below:

class MyModel(models.Model):
    field_1 = models.BigIntegerField(max_digits=5) # Here
    field_2 = models.IntegerField(max_digits=5) # Here
    field_3 = models.SmallIntegerField(max_digits=5) # Here
    field_4 = models.PositiveBigIntegerField(max_digits=5) # Here
    field_5 = models.PositiveIntegerField(max_digits=5) # Here
    field_6 = models.PositiveSmallIntegerField(max_digits=5) # Here

So, I replace them with DecimalField as shown below, then the error above was solved:

class MyModel(models.Model):
    field = models.DecimalField(max_digits=5, decimal_places=2)
Answered By: Kai – Kazuya Ito
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.