#9.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
#9.1.1. One-to-One (1:1) Relationships
Example of 1:1 Server to Admin Relationship
... from Products.ZenRelations.RelSchema import *#... class Server(Device): ... _relations = (# ("admin" #, ToOne(ToOne, "Admin", "server")#), ) + Device._relations ... ... class Admin(TestBaseClass): ... _relations = ( ("server", ToOne(ToOne, "Server", "admin")),# ) ... ... ...
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:
Importing ToOne from Products.ZenRelations.RelSchema. | |
Appending a two-item tuple to the _relations attribute | |
The first item in the tuple is a "string" object which is the local name | |
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:
| |
Appending a complementary two item tuple to the _relations attribute in the relative class. |