gpt4 book ai didi

python - Plone-为什么我收到 WrongContainedType 错误?

转载 作者:行者123 更新时间:2023-12-03 02:24:24 25 4
gpt4 key购买 nike

我有一个名为 IReportSettings 的接口(interface),用于具有元组的注册表项,该元组存储使用接口(interface) IUserSetting 的 PersistantObjects,该接口(interface)由名为 UserSetting 的对象类型实现。 IUserSetting 和 UserSetting 的工厂适配器通过 registerFactoryAdapter 注册。当我尝试使用 UserSettings 元组设置注册表项的 IReportSettings 时,出现错误:

WrongContainedType: ([WrongContainedType([WrongType('uname', <type 'unicode'>,'user_name')],'')],'value')

这是我的一些代码:

class PersistentObject(PersistentField, schema.Object):
pass

class IUserSetting(Interface):
user_name = schema.TextLine(title=u"User",
required=True,
default=u"",
)

field_a= schema.List(title=u"Field A",
value_type=schema.Choice(vocabulary=u'my.product.vocabularies.SomeVocabulary'),
required=False,
default=None
)

field_b = schema.TextLine(title=u"Field B",
required=False,
default = u"",
)
.
.

class UserSetting(object):
implements(IUserSetting)

def __init__(self, user_name=u'', field_a=None, field_b=u'', ..):
self.user_name = user_name
self.field_a = field_a
if field_a=None:
self.field_a = []
self.field_b = field_b
..

registerFactoryAdapter(IUserSetting, UserSetting)

class IReportSettings
settings = schema.Tuple(
title=u"User settings for a Report",
value_type=PersistentObject(
IUserSetting,
title=u"User Setting",
description=u"a Report Setting"
),
required=False,
default=(),
missing_value=(),
)

在表单类中:

def saveUI(self, data):
user_name = api.user.get_current().id
field_a = data['field_a']
field_b = data['field_b']
.
.
registry_util = queryUtility(IRegistry)
user_settings = registry_util.forInterface(IReportSettings,check=False)
already_exists = False
#Iterate through user_settings.settings and check if a setting for the user already exists, update if it exists or add new if it doesn't exist

if already_exists == False:
#Add new UserSetting - Example Data
new_setting = UserSetting(user_name=user_name, field_a= field_a, field_b=field_b)
user_settings.settings += (new_setting,)

new_setting 是我打印时的正确类型。我传递的哪些数据可能是错误的?

在我的registry.xml 文件中,我添加了:

<records interface="my.package.user_settings.IReportSettings" purge="False"/>

另外,我的代码基于这篇文章: http://blog.redturtle.it/plone.app.registry-how-to-use-it-and-love-it

最佳答案

我是这篇文章的作者,但我认为您的问题与此无关。

存储在user_name中的数据必须是unicode,但调用api.user.get_current().id将(可能)返回一个8位字符串。

所以尝试将行更改为:

user_name = api.user.get_current().id.decode('utf-8')

关于python - Plone-为什么我收到 WrongContainedType 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31994599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com