Django - 必须是 unicode,而不是 None

Django - must be unicode, not None

我正在生成一个 csv 文件,但在我的数据库的某些字段中是空的,当我创建 csv 文件时,此错误显示 "must be unicode, not 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)

我能做什么?我尝试检查值是否为 none 并返回 "value = "none" " 但它显示此错误,"must be unicode not str" 回溯:

Environment:

Request Method: GET Request URL: http://127.0.0.1:8000/admin/descargar_clientes

Django Version: 1.7.1 Python Version: 2.7.6 Installed Applications: ('django.contrib.admin.apps.SimpleAdminConfig', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'adminplus', 'clients', 'currier') 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/eloy/jet_dev/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/eloy/jet_dev/local/lib/python2.7/site-packages/django/utils/decorators.py" in _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" in _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" in inner 204. return view(request, *args, **kwargs) File "/home/eloy/jet/currier/admin.py" in descargar_clientes 126. encode_notnull(normalize('NFKD', client.profesion).encode('ascii', 'ignore')) or "",

Exception Type: TypeError at /admin/descargar_clientes Exception Value: must be unicode, not None

您的编写器只需要字符串,而您显然向其发送了一个 None,这就是问题的原因。

要覆盖它,请更改

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

client.celular or ""

这样,如果 client.celular 的计算结果为 None,则 writerow 接收到的值是 "",它仍然是一个字符串。或者 u"" 如果你希望它是 unicode。