[Zope3-Users] z3c.form: Data Manager - getting value via dm.query() instead of dm.get()

Hermann Himmelbauer dusty at qwer.tk
Thu Aug 14 07:42:30 EDT 2008


> Hi Hermann
>> So, it may happen, that not all fields are filled out,
>> therefore key/value pairs may be missing. If that happens,
>> the widget routine throws an error.
>> If I manage to overcome this error (e.g. by replacing the
>> get() through
>> query() routine in z3c.form.widget), my form works.
>
> I understand. Ther must be something wrong.
> Take a look at the AddForm implementation this one uses
> ignoreContext = True. This forces to not store the values
> to the object. Because it doesn't use applyChanges like
> the EditForm does.
>
> For a SearchForm, you can use extractData offered from
> the form and do what every you need to do with the extracted
> form values. e.g. store them into a session. You can setup
> the widgets with this values later.

Yes, I already did something similar - I developed a custom form class
that inherits from form.Form and sets ignoreContext=True.
The problem is how to setup widgets with their default values. In
form.widget, there seem to be the following possibilities:

1) From request (Not applicable here)
2) From the data manger (As I tried)
3) From the default field value (Not possible here, as the value is not
static)
4) Via an adapter providing IValue (Possible, but very complicated)

So, only (3) seems to be the way to go, which confronts me with the above
problems.

> Or even better you can define a custom ISession object
> which provides what you need. e.g. with useful default
> values from the schema field definition. Such a custom
> session object can be used within the EditForm and
> you don't have to use the AddForm concept.
>
>
> Such a search session object could look like:
>
> class ISearchSession(ISession):
>     """Message filter session."""
>
>     searchText = zope.schema.TextLine(
>         title=Search text',
>         description=u'The search text',
>         default=u'',
>         required=False)
>
> class SearchSessioon(Session):
>     _spd = None
>
>     @Lazy
>     def spd(self):
>         return self.__getitem__('SEARCH_SESSION_KEY')
>
>     @apply
>     def searchText():
>         def get(self):
>             return self.spd.get('searchText', u'')
>         def set(self, searchText):
>             self.spd['searchText'] = searchText
>         return property(get, set)

Is the parent object Session/ISession the one from zope.session? How does
it connect to the persistent Session object? And what is "@Lazy"?

Yes, that's a possibility, but it would force me to write:

- An property for every field.
- A class for every search view.

All in all that's around ~ 200 lines of code for 5 search forms with appr.
5 fields each, compared to appr. 10 lines of code in my solution.

> I allways use such a session implementation because it
> abstracts what you need, uses schema fields, offers
> built in defaults and works well with z3c.form.

Yes, thanks for the code snippet, it's interesting and a clean solution.

> Note; I'm still not 100% sure if the get() or query()
> usage in applyChanges is right or wrong. I hope to
> find time to deep into that in the near future.
>
> But I think nobody will understand the concept if we
> implement a form property for use get or query in
> applyChanges.

Yes, that may be true. Hmmm...

> Hope the session concept works for you till someone
> agrees/checks the get/query useage.

Well, I have two more possibilities until this is sorted out entirely:

1) Monkey patch z3c.form.DictionaryField.get() and let it return
interfaces.NOVALUE
2) Write an register my own data manager to overcome this.

Thanks again for help,
Best Regards,
Hermann

-- 
hermann at qwer.tk
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7




More information about the Zope3-users mailing list