Philip Bauer: Why relations are awesome
Talk by Philip Bauer at the online Plone Conference 2021.
Relations in Plone are incredibly powerful. It has lots of sophisticated options. But Plone does not use most of them. Plone here is like a toddler in a Lamborghini.
You can use relations in fields, and Plone also uses two relations without fields:
- working copy (iterate)
- linkintegrity checking
There are at least nine packages required to build one simple relation. Is that okay? My answer: yes, that's all fine. Why? It is stable, very fast, very flexible (more than Plone needs), and documented. Well, the documentation is difficult.
What's new in Plone?
We now have a sane Python api for relations:
api.relation.create api.relation.get api.relation.delete
Get all friends who have a relation with Bob:
api.relation.get(target=bob, relationship="friend)
Get all relations of Alice:
api.relation.get(source=alice)
Create a relation between Bob and Alice and store it on a field with name "friend" if it exists:
api.relation.create(source=bob, target=alice, relationship="friend")
You can use this in Plone 5 on Python 3. On Python 2 you can use the similar collective.relationhelpers.
- We have a controlpanel for relations.
You can inspect relations and back relations. It also has a magic "Rebuild" button. This will purge all relations, and restore them. This is useful, especially after an in-place migration, to get rid of broken relations.
In Plone 6 this is built in. In Plone 5 you can use `` collective.relationhelpers`` but you need to remember the url of the controlpanel.
- Helpers to manage, inspect and rebuild relations.
See from Products.CMFPlone import relationhelper.
- Better and more relation fields
There are default relation fields with default widgets, but you now have more choices for the widgets.
A nice helper is the new StaticCatalogVocabulary that you can use a vocabulary.
Documentation and examples:
You can also do relation fields without relations: store a uuid instead of a relation value. You can use a schema.Choice field with a RelatedItemsFieldWidget. But watch out: when you delete a content item that this field points to, Plone will do no cleanup for you. For relations, this does happen.
What about Volto?
- The related items field is not displayed.
- RelationChoice is broken
- None of the new widget types work
We need to fix those, and I have filed issues for them. Also, we may want to have an endpoint in plone.restapi to query relations.