1. How to Add a Zen Model Relationship

In this "How To", you will learn to create a One-to-One Relationship, a One-to-Many Relationship, a Many-to-Many Relationship, and finally a One-to-Many Container Relationship.

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.