1

我正在使用 django-import-export 导入 csv 文件。我的模型中有一个FloatField

模型.py

purchase_price = models.FloatField(null=True, blank=True)

当我导入具有空白值的 csv 文件时,它会引发错误:

/admin/csv_imp/book/process_import/ 处的 ValueError

无法将字符串转换为浮点数:

Request Method:     POST
Request URL:    http://localhost:8000/admin/csv_imp/book/process_import/
Django Version:     1.7.1
Exception Type:     ValueError
Exception Value:    

could not convert string to float: 

Exception Location:     /home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 1550
Python Executable:  /home/bgdev/Desktop/virtualenvs/django17/bin/python
Python Version:     2.7.6
Python Path:    

['/home/bgdev/Desktop/virtualenvs/django17/test_pro',
 '/home/bgdev/Desktop/virtualenvs/django17/src/admin-bootstrap',
 '/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7',
 '/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/plat-i386-linux-gnu',
 '/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/lib-tk',
 '/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/lib-old',
 '/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/lib-dynload',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-i386-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages',
 '/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/site-packages']

Server time:    Tue, 9 Dec 2014 05:33:36 +0000



Environment:


    Request Method: POST
    Request URL: http://localhost:8000/admin/csv_imp/book/process_import/

    Django Version: 1.7.1
    Python Version: 2.7.6
    Installed Applications:
    ('bootstrap_admin',
     'import_export',
     'django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'csv_imp')
    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')


    Traceback:
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
      111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
      105.                     response = view_func(request, *args, **kwargs)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
      52.         response = view_func(request, *args, **kwargs)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
      204.             return view(request, *args, **kwargs)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/admin.py" in process_import
      130.                                  raise_errors=True)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/resources.py" in import_data
      359.                     six.reraise(*sys.exc_info())
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/resources.py" in import_data
      345.                         self.save_instance(instance, real_dry_run)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/resources.py" in save_instance
      163.             instance.save()
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in save
      591.                        force_update=force_update, update_fields=update_fields)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
      619.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
      681.                                       forced_update)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_update
      725.         return filtered._update(values) > 0
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/query.py" in _update
      600.         return query.get_compiler(self.db).execute_sql(CURSOR)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
      1004.         cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
      775.             sql, params = self.as_sql()
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
      969.                 val = field.get_db_prep_save(val, connection=self.connection)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
      627.                                       prepared=False)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
      619.             value = self.get_prep_value(value)
    File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
      1550.         return float(value)

    Exception Type: ValueError at /admin/csv_imp/book/process_import/
    Exception Value: could not convert string to float: 
4

2 回答 2

0

我发现和 IntegerField 有类似的问题。作为 csv 文件中的最后一列,事实证明我在数据集中引入了一个额外的行尾字符,即使该字段的值是空的。在将其添加到数据集之前从每一行中删除 EOL 字符修复了该问题:

for line in file.readlines():
    line = line.rstrip('\r\n')
    dataset.append(tuple(item for item in line.split(',')))
于 2015-01-22T10:57:01.313 回答
0

此链接将向您展示 null 和空白的作用:在 django 中区分 null=True,blank=True

如果您可以安排您的 csv 文件在末尾包含所有空值,则可以删除逗号。那可能行得通。或者也许创建一个临时模型,将这些字段分配给 Charfield。然后将适当的字段复制到您的最终模型

这是未经测试的:

update <table> set <field> = 0 where id in (select id from <table> where <field> = "");

根据需要填写您的表名和字段名。您可以从您的 sql 命令行程序运行它

于 2014-12-08T14:00:26.627 回答