Django - 必须是unicode,而不是None

时间:2015-02-10 16:26:37

标签: python django csv unicode

我正在生成一个csv文件,并且在我的数据库的某些字段中是空的,当我创建csv文件时,此错误显示"必须是unicode,而不是None"

这就是我在做什么

def encode_notnull(field):
    if field:
        field.encode("utf-8")
    return field

clients = Usuario.objects.all()
    for client in clients:
        row = [
                client.casillero,
                client.pk,
                encode_notnull(client.tipo_rif),
                encode_notnull(client.rif),
                encode_notnull(client.razon_social),
                '',
                client.fecha_registro,
                encode_notnull(client.direccion_fiscal),
                client.codigo_telf_empresa,
                client.telf_empresa,
                client.codigo_fax_empresa,
                client.fax_empresa,
                client.correo_empresa,
                client.web,
                client.tipo_cedula,
                client.cedula,
                encode_notnull(normalize('NFKD', client.user.last_name).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.user.first_name).encode('ascii', 'ignore')),
                client.fecha_nacimiento,
                encode_notnull(normalize('NFKD', client.profesion).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.pais).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.estado).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.ciudad).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.urbanizacion).encode('ascii', 'ignore')),
                client.codigo_postal,
                encode_notnull(normalize('NFKD', client.avenida).encode('ascii', 'ignore')),
                client.codigo_telefono,
                client.telefono,
                encode_notnull(normalize('NFKD', client.tipo_inmueble).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.inmueble).encode('ascii', 'ignore')),
                client.codigo_celular,
                client.celular,
                client.codigo_fax,
                client.fax,
                encode_notnull(normalize('NFKD', client.user.email).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.user.username).encode('ascii', 'ignore')),
                encode_notnull(normalize('NFKD', client.user.password).encode('ascii', 'ignore')),
                encode_notnull(client.status),
                encode_notnull(client.tipo_cliente),
                client.autorizado,
                client.jet_info,
                encode_notnull(client.observaciones),
                encode_notnull(client.codigo_empresa),
                encode_notnull(client.cargo),
                encode_notnull(client.sexo),
                encode_notnull(client.departamento),
                client.celular,#decia cedularl ???
                '',
                '',
                '',
                encode_notnull(client.tomo),
                client.ano,
                client.representante_legal,
                encode_notnull(client.plan_inscripcion),
                client.numero_afiliado,
                encode_notnull(client.referido),
                ''
            ]

        writer.writerow(row)

我该怎么办?我尝试检查值是否为无并返回" value =" none" "但是它显示了这个错误,"必须是unicode而不是str" 回溯:

  

环境:

     

请求方法:GET请求URL:   http://127.0.0.1:8000/admin/descargar_clientes

     

Django版本:1.7.1 Python版本:2.7.6已安装的应用程序:   (' django.contrib.admin.apps.SimpleAdminConfig&#39 ;,   ' django.contrib.auth',' django.contrib.contenttypes',   ' django.contrib.sessions',' django.contrib.messages',   ' django.contrib.staticfiles',' adminplus',' clients',' currier')   已安装的中间件:   (' django.contrib.sessions.middleware.SessionMiddleware&#39 ;,   ' django.middleware.common.CommonMiddleware&#39 ;,   ' django.middleware.csrf.CsrfViewMiddleware&#39 ;,   ' django.contrib.auth.middleware.AuthenticationMiddleware&#39 ;,   ' django.contrib.auth.middleware.SessionAuthenticationMiddleware&#39 ;,   ' django.contrib.messages.middleware.MessageMiddleware&#39 ;,   ' django.middleware.clickjacking.XFrameOptionsMiddleware&#39)

     

回溯:文件   " /home/eloy/jet_dev/local/lib/python2.7/site-packages/django/core/handlers/base.py"   在get_response中     111. response = wrapped_callback(request,* callback_args,** callback_kwargs)File" /home/eloy/jet_dev/local/lib/python2.7/site-packages/django/utils/decorators.py"   在_wrapped_view中     105. response = view_func(request,* args,** kwargs)File" /home/eloy/jet_dev/local/lib/python2.7/site-packages/django/views/decorators/cache.py" ;   在_wrapped_view_func中     52. response = view_func(request,* args,** kwargs)File" /home/eloy/jet_dev/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" ;   在内心     204. return view(request,* args,** kwargs)File" /home/eloy/jet/currier/admin.py"在descargar_clientes     126. encode_notnull(normalize(' NFKD',client.profesion).encode(' ascii',' ignore'))或"" ,

     

异常类型:/ admin / descargar_clientes异常时的TypeError   值:必须是unicode,而不是None

1 个答案:

答案 0 :(得分:4)

你的作者只期待字符串,你明显发送了None,这就是问题的原因。

要覆盖此内容,请更改

 client.celular #for example, assuming this is the line the error occurs

client.celular or ""

这样,如果client.celular评估为None,则writerow收到的值为"",仍为字符串。或u""如果你想让它成为unicode。