Search code examples
pythongoogle-app-enginegoogle-cloud-datastorewebapp2

BadValueError: Property X must be a str or unicode instance, not a tuple (on update, not on creation)


GAE is driving me crazy.

I have the following:

    if id_puertas[x]:
        puerta = Puerta.get_by_id(int(id_puertas[x]))
        puerta.ubicacion = ubicacion_puertas[x],
        puerta.ancho = float(ancho_puertas[x]),
        puerta.marco = float(marco_puertas[x]),
        puerta.alto  = float(alto_puertas[x]),
        puerta.giro  = giro_puertas[x],
        puerta.condena = True if condena_puertas[x] == 'Si' else False,
        puerta.extra = extra_puertas[x]
    else:
        puerta = Puerta(
            medicion = medicion.key().id(),
            ubicacion = ubicacion_puertas[x],
            ancho = float(ancho_puertas[x]),
            marco = float(marco_puertas[x]),
            alto  = float(alto_puertas[x]),
            giro  = giro_puertas[x],
            condena = True if condena_puertas[x] == 'Si' else False,
            extra = extra_puertas[x]
        )
    puerta.put()

The same form sends either id_puerta as a number, or blank. If it's a number, updates an entity in the datastore. If it's blank, creates a new entity.

Creation works perfectly.

But if I resend the same form (even without modifications) the update chokes on the first assignment.

ubicacion_puertas[x] is a string (the same string on creation than on update), and puerta.ubicacion is a StringProperty, but I receive the following error:

 BadValueError: Property ubicacion must be a str or unicode instance, not a tuple

In the debugger I see clearly that ubicacion_puertas[0] is u'Salon'. so I can't make sense of this error.


Solution

  • When you do:

    puerta.ubicacion=ubicacion_puertas[x],
    

    you're storing a tuple in the ubicacion attribute of puerta: another more readable form is:

    puerta.ubicacion=(ubicacion_puertas[x],)
    

    Are you sure this is what you want? Just get rid of the , to store a str

    puerta.ubicacion = ubicacion_puertas[x]
    

    (the same goes for the other attributes...)