导入FloatField时出错,使用django-import-export

时间:2014-12-08 12:34:18

标签: python django django-models django-admin django-import-export

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

models.py

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

当我导入带有空值的csv文件时,会抛出错误:

/ admin / csv_imp / book / process_import /

中的ValueError

无法将字符串转换为float:

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: 

2 个答案:

答案 0 :(得分:0)

此链接将显示null和空白的内容:differentiate null=True, blank=True in django

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

这是未经测试的:

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

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

运行它

答案 1 :(得分:0)

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

for line in file.readlines():
    line = line.rstrip('\r\n')
    dataset.append(tuple(item for item in line.split(',')))