[Grok-dev] Fields() vs AutoFields()

Jan-Wijbrand Kolman janwijbrand at gmail.com
Thu Jan 22 14:53:13 EST 2009


Jeroen Michiel wrote:
> What is the difference exactly between grok.Fields() and grok.AutoFields().
> It seems I can use them interchangeably, but as they exist, I figure they
> both have a separate purpose, but the documentation is not exactly clear on
> it (or it eludes me).

They're not really interchangeable. That is, grok.Fields() is used for
building forms where there is no schema (an interface with schema
fields) in play. grok.AutoFields is intended to be used for building
form where there is one.

Compare:

  import grok
  from zope import schema

  class SomeAdHocForm(grok.AddForm):

    form_fields = grok.Fields(
      name=schema.TextLine(title=u'Name', required=True),
      surname=schema.TextLine(title=u'Surame', required=True),
      age=schema.Int(title=u'Age', required=True)
      )

to:

  from zope import interface

  class IPersonalInfo(interface.Interface):
    name = schema.TextLine(title=u'Name', required=True)
    surname = schema.TextLine(title=u'Surame', required=True)
    age = schema.Int(title=u'Age', required=True)

  class SomeSchemaBasedForm(grok.AddForm):

    form_fields = grok.AutoFields(IPersonalInfo)

or even to:

  class PersonalInfo(grok.Model):
    interface.implements(IPersonalInfo)

  class AnotherSchemaBasedForm(grok.AddForm):

    form_fields = grok.AutoFields(PersonalInfo)

or again even to:

  class PersonalInfo(grok.Model):
    interface.implements(IPersonalInfo)

  class YetAnotherSchemaBasedForm(grok.AddForm):
    grok.context(PersonalInfo)

That's what grok.Fields cannot do...

I hope this helps a bit... Some of this is illustrated in more detail in
 grokcore.formlib's tests BTW.


regards,
jw



More information about the Grok-dev mailing list