[Grok-dev] Problems rendering zope.schema.List
Leonardo Rochael Almeida
leorochael at gmail.com
Thu Jul 5 09:41:59 EDT 2007
On 7/5/07, Luciano Ramalho <luciano at ramalho.org> wrote:
> This is a gotcha that got me before... Philikon helped me: add a
> default argument to the list constructor, like this:
>
> creators = schema.List(title=u"Authors", required=False,
> value_type=schema.TextLine(), default=[])
>
> The problem is that the list must never be None. When empty, it must be [].
>
> IMHO, it's a usability bug in the Zope3 API. If the list fails
> miserably when it's None, then the default=[] should be automatic. Or
> is there some ratinonale for the way things are?
I suppose the problem is that you're specifying a mutable object as
the default for a field. This could cause you problems if someone
mutates this value afterward.
In a simple web app where all interactions are caused by form
handling, it could well be the case that no one will mutate the
default object (I think the default widget always creates a new list
when returning the value). But suppose some piece of code takes an
object that was set with the default value and manipulates the list
directly, this would change the default value for the schema itself.
All schema fields like List contain a reference to the type of the
object they're supposed to create (defaults to the standard "list"). I
think the proper default behaviour, in this case, would be to set the
default value with a fresh instance of the underlying type. This could
be done by making the "default" attribute of the schema field instance
be a property.
Cheers, Leo
More information about the Grok-dev
mailing list