0
votes

im generating a csv file, and in some fields of my DB are empty, and when im creating the csv file this error shows "must be unicode, not None"

here's what im doing

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)

what can i do? i've try checking if the value is none and returning "value = "none" " but it shows this error, "must be unicode not str" traceback:

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

1

1 Answers

4
votes

Your writer is expecting strings only, and you are clearly sending it a None which is the cause of the issue.

To override this, change

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

to

client.celular or ""

This way, if client.celular evaluates to None, the value writerow receives is "" which is still a string. or u"" if you want it to be unicode.