I have this user schema defined in a plone add-on to be used for multiple websites.
class IUser(Interface):
userid = schema.TextLine(
title=_("User id"),
required=True,
constraint=validate_userid,
)
email = schema.TextLine(
title=_(u"Email"),
required=True,
constraint=validate_email
)
optional_type = schema.Choice(
title=_(u"User type"),
vocabulary="user_types",
required=True,
)
Sometimes the optional_type field is needed sometimes not. user_types is saved in portal_vocabularies. I want the field to be used only when the vocabulary exists and I want it to be ignored when the definition is missing.
I mean, I want this field to work for websites where it is used, but the user schema also to work in other case. For the moment I receive this error: ComponentLookupError: (<InterfaceClass zope.schema.interfaces.IVocabularyFactory>, 'user_types').
I know I can create an empty unused vocabulary, but do you have any better solution here?
Not possible, but you can skip the error and make things look like the field doesn't exist. Good to know:
So you can solve this without defining a vocabulary in portal_vocabularies. Just define a factory like:
foo.py:as utility:
configure.zcml:Then you can hide the field and just ignore it everywhere is not needed.