Thaian
Thaian

Reputation: 1245

Error for my model: invalid literal for int() with base 10: ''

I recive error: invalid literal for int() with base 10: ''

When I'm trying add object to my model(table). I tried do this in shell too but is this same situation. I checked my model and every field and i have only optional field which are PositiveIntegerField and its evertything.

My model:

class Client(models.Model):
    id = models.OneToOneField(User, on_delete=models.CASCADE, unique=True, primary_key=True)
    uuid = models.UUIDField(default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=256, unique=True)
    vat = models.CharField(max_length=50, blank=True, null=True, unique=True)
    regon = models.PositiveIntegerField(blank=True, null=True, default='')
    krs = models.PositiveIntegerField(blank=True, null=True, default='')
    address = models.CharField(max_length=64)
    zip_code = models.CharField(max_length=10)
    city = models.CharField(max_length=64)
    country = models.ForeignKey(CountriesChoices, blank=False)
    service = models.ForeignKey(ClientServiceChoices, blank=False, null=False)
    modified_date = models.DateTimeField(auto_now=True, blank=True, editable=False)
    created_date = models.DateTimeField(auto_now_add=True, blank=True, editable=False)

And my traceback:

Environment:

Django Version: 1.8.8
Python Version: 3.5.1
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'core',
 'api',
 'client',
 'registration',
 'avatar',
 'filer',
 'mptt',
 'easy_thumbnails',
 'reversion',
 'reportlab',
 'mail_templated',
 )
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\loc\PycharmProjects\project\api\views.py" in import_data
  505.                 client.save()
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in save
  734.                        force_update=force_update, update_fields=update_fields)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in save_base
  762.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in _save_table
  827.                                       forced_update)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\base.py" in _do_update
  877.         return filtered._update(values) > 0
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\query.py" in _update
  580.         return query.get_compiler(self.db).execute_sql(CURSOR)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1062.         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  829.             sql, params = self.as_sql()
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
  1030.                 val = field.get_db_prep_save(val, connection=self.connection)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
  710.                                       prepared=False)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
  702.             value = self.get_prep_value(value)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  1868.         return int(value)

Exception Type: ValueError at /api/import/
Exception Value: invalid literal for int() with base 10: ''

This model works fine on my client add form but in shell and in view in this case doesn't work. Do u have idea why? It's problem because this error don't show nothing more :(

Upvotes: 0

Views: 1010

Answers (1)

AKS
AKS

Reputation: 19811

Your default value is a string here which is leading to this error:

regon = models.PositiveIntegerField(blank=True, null=True, default='')
krs = models.PositiveIntegerField(blank=True, null=True, default='')

You are getting the error because following statement cannot convert blank string to int:

return int(value)

You can test yourself in console:

>>> int('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

Upvotes: 2

Related Questions