python - How can I create a z3c.form that has multiple schemas? -
i using cms plone build form contains 2 other schemas.
with group forms, have been able include both of fields of 2 schemas. however, lose of properties such hidden or when use datagridfield build table. want able have both of these forms fields , on save able save them in object link clicked parent -> object 1 [top of form] -> object 2 [bottom of form]
here python code:
class questionpart(group.group): label = u'question part' fields = field.fields(iquestionpart) template = viewpagetemplatefile('questionpart_templates/view.pt') class question(group.group): label = u'question' fields = field.fields(iquestion) template = viewpagetemplatefile('question_templates/view.pt') class questionsinglepart(group.groupform, form.addform): grok.name('register') grok.require('zope2.view') grok.context(isiteroot) label = u"question single part" ignorecontext = true enable_form_tabbing = false groups = (question,questionpart) def update(self): super(questionsinglepart, self).update()
this code displays both fields of iquestion, iquestionpart without regard things like: form.mode(contype='hidden') or datagridfield widget. found way displays correct form field hints.
class questionsinglepart(autoextensibleform, form.addform): grok.require('zope2.view') grok.context(isiteroot) label = u"question" schema = iquestion additionalschemata = (iquestionpart,)
i feel still long way off. have talked people. trying use separate form , view.
so far, @ point code:
class questionsinglepartform(autoextensibleform, form.form): ignorecontext = true autogroups = true template = viewpagetemplatefile('questionsinglepart_templates/questionsinglepartform.pt') @property def additionalschemata(self): return self._additionalschemata def __init__(self, context, request, schema, additional=()): self.context = context self.request = request if not iinterface.providedby(schema): raise valueerror('schema not interface object') self._schema = schema if not all(iinterface.providedby(s) s in additional): raise valueerror('additional schema not interface') self._additionalschemata = additional class questionsinglepartview(object): schema = iquestion additional = (iquestionpart,) def __init__(self, context, request): self.context = context self.request = request self.form = questionsinglepartform(context, request, self.schema, self.additional) def magic(self, data, errors): pass """ question = question() question.number = data['number'] question.questioncontent = data['questioncontent'] questionpart = questionpart() questionpart.typeofquestion = data['iquestionpart.typeofquestion'] questionpart.explanation = data['iquestionpart.explanation'] questionpart.filesize = data['iquestionpart.filesize'] questionpart.filetype = data['iquestionpart.filetype'] questionpart.hints = data['iquestionpart.hints'] questionpart.table = data['iquestionpart.table'] questionpart.contype = data['iquestionpart.contype'] questionpart.content = data['iquestionpart.content'] """ def update(self, *args, **kwargs): if self.request.get('request_method') == 'post': data, errors = self.form.extractdata() self.magic(data, errors) self.formdisplay = self.form.render() def __call__(self, *args, **kwargs): self.update(*args, **kwargs) return self.index(*args, **kwargs)
i struggling rendering of form , questionsinglepart object not have index attribute.
after couple of hours of working plone devs, have figured out going on.
i had left out:
@property def schema(self): return self._schema
i needed define index in view so:
index = viewpagetemplatefile('questionsinglepart_templates/questionsinglepart.pt')
i needed add views init:
alsoprovides(self.form, iwrappedform)
in update method view needed call before formdisplay. remove data extraction , move form.
def update(self, *args, **kwargs): self.form.update(*args, **kwargs) self.formdisplay = self.form.render()
i still working data save objects.
only form classes include plone.autoform.base.autoextensibleform
mixin pay attention schema form hints. try using mixin group form class (and provide schema
attribute mixin looks instead of fields
):
from plone.autoform.base import autoextensibleform class questionpart(autoextensibleform, group.group): label = u'question part' schema = iquestionpart template = viewpagetemplatefile('questionpart_templates/view.pt')
Comments
Post a Comment