我发现错误: 对于带有基数为10的int(),文字无效:''
当我尝试将对象添加到我的模型(表格)时。我尝试在shell中也这样做但是同样的情况。我检查了我的模型和每个字段,我只有可选字段,它们是PositiveIntegerField及其evertything。
我的模特:
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)
我的追溯:
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: ''
这个模型在我的客户端添加表单上运行正常,但在shell中,在这种情况下在视图中不起作用。你知道为什么吗?这是一个问题,因为这个错误不再显示:(
答案 0 :(得分:2)
此处default
值为string
,导致此错误:
regon = models.PositiveIntegerField(blank=True, null=True, default='')
krs = models.PositiveIntegerField(blank=True, null=True, default='')
您收到错误,因为以下语句无法将空字符串转换为int:
return int(value)
您可以在控制台中测试自己:
>>> int('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''