Django invalid literal for int() with base 10:

400 views Asked by At

I want to import data from a csv file to a model. So I have this code which does the job for me:

Import.py

import csv
with open('usuarios.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        created = Users.objects.create(
                    field1=row[0],
                    field2=row[1],
                    field3=row[2],
                    )

And my model:

class Users(AbstractBaseUser):
    field1 = models.CharField(max_length=100, null=True)
    field2 = models.CharField(max_length=100, null=True)
    field3 = models.CharField(max_length=100, null=True)

But for some reason, when I execute import.ty the console handles this:

  File "<console>", line 1, in <module>
  File "<string>", line 25, in <module>
  File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 154, in
 get_or_create
    return self.get_queryset().get_or_create(**kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 376, in g
et_or_create
    return self.get(**lookup), False
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 301, in g
et
    clone = self.filter(*args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 593, in f
ilter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 611, in _
filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Python34\lib\site-packages\django\db\models\sql\query.py", line 1204,
 in add_q
    clause = self._add_q(where_part, used_aliases)
  File "C:\Python34\lib\site-packages\django\db\models\sql\query.py", line 1240,
 in _add_q
    current_negated=current_negated)
  File "C:\Python34\lib\site-packages\django\db\models\sql\query.py", line 1131,
 in build_filter
    clause.add(constraint, AND)
  File "C:\Python34\lib\site-packages\django\utils\tree.py", line 104, in add
    data = self._prepare_data(data)
  File "C:\Python34\lib\site-packages\django\db\models\sql\where.py", line 79, i
n _prepare_data
    value = obj.prepare(lookup_type, value)
  File "C:\Python34\lib\site-packages\django\db\models\sql\where.py", line 352,
in prepare
    return self.field.get_prep_lookup(lookup_type, value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 1085, in get_prep_lookup
    return super(IntegerField, self).get_prep_lookup(lookup_type, value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 369, in get_prep_lookup
    return self.get_prep_value(value)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
 1079, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: '(3482) 453874'

Ideas ? I've been reading that it could have something to be with the primary_key, but I dont know what exactly

1

There are 1 answers

1
Animesh Sharma On

Try this:

import csv
with open('usuarios.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        kwargs = {field1=row[0],
                  field2=row[1],
                  field3=row[2],}
        created = Users(**kwargs)
        created.save()