Models in Grok, are classes which define the attributes and methods of objects identified at run time to serve as context for HTTP requests. To properly understand the nature of models, one must realise that were one to declare:
...that the class definition only specifies what an Article
instance should look like. It's just a class (or type) declaration for something which implements an object conforming to an IArticle
.
At run time, while processing requests, we are dealing with instances of Article
, which now provide an IArticle
interface. The Zope publisher is aware of the objects registered in the ZCA registry, and can process a URL to identify an object instance and a method (or view on the object).
The data associated with an Article
instance may originate from a number of sources. If the Article
is derived from persistent.persistent, (for example, a grok.Model vs an object) then the Article
instance may be stored in the ZoDB. Alternately, the Article instance
could represent a row in a relational database table, or the Article
instance could be created dynamically (eg. myArticle=Article()
).
Dynamically created data models instances are called transient. Such data lives only as long as the request does unless persisted in one or another database.
If an Article
instance was retrieved from the ZoDB, then it is most likely (but not always) retrieved as a result of the Zope publisher traversing the URL to locate the model. At the same time, the publisher would identify a view, either by name from the URL, or use the default view named 'index'. Views in Grok are also classes registered with the ZCA:
The view is registered with the ZCA object registry as an adapter by name ('index'), and having object interfaces (Article
) and what the view is instantiated from (an IBrowserRequest
). So, when processing a Request
, the Zope publisher can easily find the 'index' view for an IArticle
and instantiate the view with an instance of Article
as context.
Specifically, a view is looked up by the Zope publisher using a function
zope.component.getMultiAdapter((Article, Request), name='index')
The view is registered with the component registry when Grok parses the module and finds a declaration for the class Index(grok.View)
with an appropriate context.