How can I change fieldsets order?
Asked Answered
K

1

7

I have a form schema which inherits from another form schema. Both have fieldsets. However, the fieldsets are put in the order they are created. So the fieldset described in the last schema will be the last one. I would like it to be the first. Is there a way to do that ?

Example:

from plone.supermodel import model
from zope import schema

class FormSchema(model.Schema):
     model.fieldset(
          'test',
          label='Test',
          fields=['field1']
     )
     field1 = schema.Text(title=u'test')


class FormSchema2(FormSchema):
     # Is last but I would like to place it first
     model.fieldset(
          'test2',
          label='Test2',
          fields=['field2']
     )
     field2 = schema.Text(title=u'test2')
Kerman answered 25/9, 2013 at 8:35 Comment(1)
I'm not sure if it works, but you could try declaring each fieldset in its own class, and mix them in a third one in the right order. Eg: class FormSchemaFinal(FormSchemaFieldset2,FormSchemaFieldset1):Choking
K
3

You can't, I am afraid. The schema fieldsets are always merged in reverse interface resolution order; base before derived interface. Declaring the fieldset again on the FormSchema2 schema will only result in the fieldset being listed twice.

If you have to control fieldset order, don't derive from the base schema but re-declare it.

Kerch answered 25/9, 2013 at 9:32 Comment(2)
This answer, of course, is not 100% factually correct. You can whack the crap out of zope interfaces by manually manipulating their internals. But you don't want to do this :)Backstretch
@Mikko: you can set the tagged values with the setTaggedValue() method on the base schema. Or monkeypatch plone.supermodel or plone.autoform. But that'd either break the base schema or requires intimate knowledge of the two frameworks and would be extremely fragile.Kerch

© 2022 - 2024 — McMap. All rights reserved.