Developer's Guide

  • Docs Home
  • Community Home

1. Add a ZenModel Relationship

The ZenRelations class allows Zope objects to form bi-directional relationships. There are four different types of relationships possible:

ONE_TO_ONE

only one object at each end of the relationship

ONE_TO_MANY

classic parent-child relation, no containment objects have different primary paths

ONE_TO_MANY_CONT

one-to-many containment relation (but bi-directional)

MANY_TO_MANY

many objects on both ends of relationship

Figure 9.1. ZenRelations

ZenRelations

1.1. One-to-One (1:1) Relationships

Example of 1:1 Server to Admin Relationship

...
from Products.ZenRelations.RelSchema import *1
...
class Server(Device):
...
_relations = (2
("admin" 3, ToOne(ToOne, "Admin", "server")4),
) + Device._relations
...
...
class Admin(TestBaseClass):
...
_relations = (
("server", ToOne(ToOne, "Server", "admin")),5
)
...
...
...

The Server object is an example of a class that inherits from Device. According to this relationship there can be only one Admin assigned to a Server and only one Server assigned to an Admin. This relationship is created by:

1

Importing ToOne from Products.ZenRelations.RelSchema.

2

Appending a two-item tuple to the _relations attribute

3

The first item in the tuple is a "string" object which is the local name

4

The second item in the tuple is a "RelSchema" object which represents the relationship to another class. In this case the ToOne constructor creates/returns that "RelSchema" object

ToOne constructors takes three parameters:

  • The first parameter is a "type" object, "remoteType" which represents the relationship from another class. The "type" should be of a class derived from RelSchema

  • The second parameter is a "string" object, "remoteClass" which is the class name of the relative. In this case it is again a ToOne relationship.

  • The third parameter is a "string" object, "remoteName" which the remote name of itself.

5

Appending a complementary two item tuple to the _relations attribute in the relative class.