[Grok-dev] 1->* relation in a shema

Steve Schmechel steveschmechel at yahoo.com
Sun Aug 9 18:29:47 EDT 2009

Hello Thomas,

I, personally, have not found an easy way to define this in the schema itself.

Mostly schema "fields" seems to give you a way to define look-up type fields that are constrained to enumerations or maybe other collections.
(It is more fully documented here: http://pypi.python.org/pypi/zope.schema#fields )

It seems to promise more, but I have not been able to make it work.
When I asked others about the same question you have, I was pointed to a much simpler solution.

You can still use schemas to define the content attributes and create auto-forms for a particular model object.  For the one-to-many relationships, you simply add containers (based on grok.container) to your object. (You will need to instantiate the containers in the object's __init__ method.)

Say you have an object named "foods", you could add two containers called "fruits" and "vegetables".  To these collections you can add more containers or models objects (based on grok.model).

The default traversal will go from the top object to the particular collection name, to the key of the object in that container.

So you could have:  


Where apple and orange are instances of a "Fruit" class that were added to the "fruits" collection contained in the "foods" object.  The "Fruit" class could have a schema that defines properties of the fruit.  

So if you are at /foods/fruits/apple the Fruit schema will be used to display the properties of the apple.
If you are at /foods/vegetables/carrots the Vegetable schema will be used to display the properties of a carrot.

(Now that I have written this far, I wish I had picked a less silly example.  Oh well.)

If you delete the "foods" object, all the contained fruits and vegetables will be deleted with it.  You can create views for the containers, if you want to list the contents of each type individually.  Or, you can customize the page template used for "foods" to display the contents of those collections right there.

If this seems to answer your question, you can see some further examples of this technique in the tutorial: http://grok.zope.org/documentation/tutorial/musical-performance-organizer-part-1

Hope this helps,

--- On Sun, 8/9/09, Thomas Bellembois <thomas.bellembois at gmail.com> wrote:

> From: Thomas Bellembois <thomas.bellembois at gmail.com>
> Subject: [Grok-dev] 1->* relation in a shema
> To: grok-dev at zope.org
> Date: Sunday, August 9, 2009, 3:22 PM
> Hello,
> I am new in the world of Grok. I am currently developping
> an application with 1->* relationships.
> How to describe this relation in the shema of the first
> entity ? Where can I find an example (the grok tutorial does
> not contain "foreign keys") ?
> Regards,
> Thomas
> -----Inline Attachment Follows-----
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> http://mail.zope.org/mailman/listinfo/grok-dev


More information about the Grok-dev mailing list