Using ReferenceField to have references to other Archetypes content items in Plone.
Archetypes comes with a kind of field called ReferenceField which is used to store references to other Archetypes objects, or any object providing the IReferenceable interface.
References are maintained in the
catalogs. You can find both at the root of your Plone
site. Check them to see their indexes and metadata.
Although you could use the ZCatalog API to manage
Archetypes references, these catalogs are rarely used
and its API is used instead.
Example declaration of a
inside a schema:
MyCTSchema = atapi.Schema(( ... atapi.ReferenceField('myReferenceField', relationship = 'somerelationship', ), ... ))
Check the Fields Reference section in the
Archetypes Developer Manual at
to learn about the
Archetypes reference fields just store the UID (Universal
Object Identifier) of an object providing the
interface. Continuing with the example above, you will
usually use the regular field API (getters/setters).
You can get the UID of a referenceable object easily:
>>> areferenceableobject_uid = areferenceableobject.UID()
To set a reference, you can use the the setter method with
either a list of UIDs or one UID string, or one object or
a list of objects (in the case the
is multi-valued) to which you want to add a reference to.
In this example we set a reference from the
object to the
To get the object(s) referenced, just use the getter. Note that what you get is the objects themselves, not their "brains":
.. TODO:: Add a glossary entry for brains.
>>> myct1.getMyReferenceField() == areferenceableobject True
Code to exercise the
API, including relationships and back-references.