corycorycory
corycorycory

Reputation: 1656

Django TypeError: argument must be int or float when trying to access model

I have a very odd error any time I try to access one of my models. All other models work fine.

Here is what my model looks like:

class Hop(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)
    aa = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True, default = 10)
    b = models.DecimalField(max_digits=15, decimal_places=2, null=True, blank=True)
    type = models.CharField(max_length=100, null=True, blank=True)
    description = models.CharField(max_length=600, null=True, blank=True)
    user_created = models.BooleanField(default=True)

    def __unicode__(self):
        return self.name

When I run Hop.objects.all() I get the following error. Never seen this before.

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 250, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 274, in __iter__
    self._fetch_all()
  File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\query.py", line 72, in __iter__
    for row in compiler.results_iter(results):
  File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\models\sql\compiler.py", line 1044, in apply_converters
    value = converter(value, expression, connection)
  File "C:\Users\corym\Documents\Projects\mybrewlab vue\Backend\mybrewlabapi\env3\lib\site-packages\django\db\backends\sqlite3\operations.py", line 285, in converter
    return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
TypeError: argument must be int or float

Upvotes: 5

Views: 4802

Answers (5)

JSotelo
JSotelo

Reputation: 19

I was converting a CharField to DecimalField and encountered this error. In one of the objects, I had a an alphabetic character. I change this field to only contain numeric characters and the change worked, I didn't have to delete any of the data or migrations.

From: ordered_quantity = models.CharField(max_length=128)

To: ordered_quantity = models.DecimalField(max_digits=100, decimal_places=4)

Upvotes: 0

Chaitanya Mogal
Chaitanya Mogal

Reputation: 398

Simply delete all files in the migrations folder(accept __init__.py) of that app and also delete db.sqlite3. And then make migrations again and also create superuser again after migrations.

It worked for me.

Upvotes: 4

Sandy
Sandy

Reputation: 224

I think you already created a model and the migration. And inserted data according to them. Then you changed the rule in model and tried to access one of the record you already created. In this case, you will get the errors like you said:

enter image description here

So the answer is, you need to update your data with new model rules

*Happy to hear that you resolved the issue.

Upvotes: 1

Sergey
Sergey

Reputation: 33

Try to run this command:

python manage.py migrate --run-syncdb

It helped me.

Upvotes: 1

corycorycory
corycorycory

Reputation: 1656

Not sure what happened, but exporting the data and re-importing fixed the error. Must have been an invalid character on either the AA or B column.

Upvotes: 0

Related Questions