【问题标题】:BadValueError: Property X must be a str or unicode instance, not a tuple (on update, not on creation)BadValueError:属性 X 必须是 str 或 unicode 实例,而不是元组(更新时,而不是创建时)
【发布时间】:2012-09-26 10:54:35
【问题描述】:

GAE 快把我逼疯了。

我有以下几点:

    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()

相同的表单发送id_puerta 作为数字或空白。如果是数字,则更新数据存储区中的实体。如果为空,则创建一个新实体。

创作完美。

但是,如果我重新发送相同的表单(即使没有修改),更新会在第一次分配时阻塞。

ubicacion_puertas[x] 是一个字符串(创建时与更新时相同),puerta.ubicacionStringProperty,,但我收到以下错误:

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

在调试器中,我清楚地看到 ubicacion_puertas[0] 是 u'Salon'。所以我无法理解这个错误。

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore webapp2


    【解决方案1】:

    当你这样做时:

    puerta.ubicacion=ubicacion_puertas[x],
    

    您在puertaubicacion 属性中存储一个元组:另一种更易读的形式是:

    puerta.ubicacion=(ubicacion_puertas[x],)
    

    你确定这是你想要的吗?只需摆脱 , 即可存储 str

    puerta.ubicacion = ubicacion_puertas[x]
    

    (其他属性也一样...)

    【讨论】:

    • 啊!谢谢。愚蠢的逗号是复制和粘贴留下的。在语法上正确没有任何东西从代码中跳出来是“错误的”,并且盯着代码太久我设法看不到它们。并且将不同的语言与不同的行尾约定混合在一起也无济于事。
    • 我敢肯定,我们都曾在某一时刻经历过“因逗号而死”...
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2016-11-18
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    相关资源
    最近更新 更多