Datagrid inside a datagrid, how to get the data from the form?

542 views Asked by At

enter image description here

I have datagrid field inside of a datagrid field (Collective.z3c.form.datagridfield)

It works from from a front end standpoint. However, when I submit the form the received data is as follows:

'questions': [{'choices': [], 'questionContent': u'Question 1 ', 'typeOfQ': None}]

Choices is the 2nd datagrid which is returned as an empty array. Is there a better to have a n number of questions with n number of choices inside? I don't know enough about subforms or widgets to know if creating either would be a solid option.

Here is the code:

class ITableRowSchema(form.Schema):
solution = schema.Bool(
    title=u'Correct Answer?', required=False,
)
trueFalse = schema.Choice(
    title=u"True or False",
    values=[('False'),('True')],
    default=('False'),
    required=False,
)
choice = schema.Text(title=u"What are the possible   choices?",required=False)


class IQuestionSchema(form.Schema):
questionContent = schema.Text(
    title=u"What is the question?",
    required=False
)

typeOfQ = schema.Choice(
    title=u"Question Type",
    values=[('True/False'),('Multiple Choice'),('Multiple Select'),('Fill-in-the-Blank'),('Short Answer')],
    required=False
)

form.widget(choices=DataGridFieldFactory)
choices = schema.List(title=u"Choices", required=False,
    value_type=DictRow(title=u"Choice #", schema=ITableRowSchema))

class IAddContent(model.Schema):
 form.widget(questions=BlockDataGridFieldFactory)
questions = schema.List(title=u"8. Create Knowledge Quiz", required=False,
 value_type=DictRow(title=u"Questions", schema=IQuestionSchema))

UPDATE: I figured out that the nested DGF never actually created a real row. They would be either TT or AA depending if you had auto-append on. These rows are ignored in the extractData(). However, if you cause the form to trigger a form violation by leaving out a required field. It builds the table again and works.

Fix: Make sure that the nested DGF gets a real row created for it and everything will work. I am working with: python, z3c.forms, plone

0

There are 0 answers