Plone Why am I getting a WrongContainedType error?

I have an IReportSettings interface for a registry key that has a tuple that stores PersistantObjects that use the IUserSetting interface, which is implemented using an object type called UserSetting. The factory adapter for IUserSetting and UserSetting is registered with registerFactoryAdapter. When I try to set the IReportSettings registry key with the UserSettings tuple, I get an error:

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

Here is my code:

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

In the form class:

 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 is the correct type when I printed it. What data can I get through, maybe I'm mistaken?

In my registry.xml I added:

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

In addition, my code is based on this article: http://blog.redturtle.it/plone.app.registry-how-to-use-it-and-love-it

+5
source share
1 answer

I am the author of the article, but I think that your problem can not cope with this.

The data stored inside your user_name must be unicode, but calling api.user.get_current().id will (probably) return an 8-bit string.

So try changing the line to:

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

All Articles