[Grok-dev] Re: A plan for removing the fields inner class support
Philipp von Weitershausen
philipp at weitershausen.de
Thu Sep 13 05:15:57 EDT 2007
Luciano Ramalho wrote:
> OK, things are not going to be as simple as I described...
>
> The ModelGrok.grok method can't be simply removed, even though the
> only thing it does is what we don't want it do do anymore.
Dude... Just remove the whole grokker! All it was there for was to set
the default values. I don't know why you wanted to keep it around at all.
> Then I started reading some of the tests which use the fields inner
> class, and realized we may want the ModelGrokker to do something for
> us after all.
>
> What if the ModelGrokker created attributes in our model class from
> the attributes declared in an interface which our model promises to
> implement?
That's a lot of magic. It may be a good automation to have, but it's
much too magical just to have it come in as a side effect of the
implements() declaration.
I'm also inclined to call YAGNI on it. Here's why:
* Usually you have an add form that creates objects with initial values
from user input. That means the models will have the necessary
attributes whenever they need to after that (e.g. for edit forms).
* If you don't have an add form, you often have a constructor (__init__)
that sets the right values. Why? Because some default values don't make
sense (None). For example, there's *no* sensible default value for the
List schema field. (An empty list is not a sensible default value
because that one list object would be shared among all classes, and a
class level list would be shared among all instances of that class).
That's why you really want to write an __init__ most of the times.
> So, for instance, this test from tests/form/form.py would pass:
>
> ######################
>
> >>> grok.grok(__name__)
> >>> manfred = Mammoth()
> >>> print manfred.name
> None
> >>> print manfred.size
> Quite normal
>
> ######################
>
> Provided that Mammoth was written like this:
>
> ######################
>
> class IMammoth(Interface):
> name = schema.TextLine(title=u"Name")
> size = schema.TextLine(title=u"Size", default=u"Quite normal")
>
> class Mammoth(grok.Model):
> implements(IMammoth)
>
--
http://worldcookery.com -- Professional Zope documentation and training
More information about the Grok-dev
mailing list