Best approach to handle broken relations in Dexterity


This is a known problem in I created an issue for it here (in 2014)

In archetypes days, when you delete content, p.a.relationfield (or actually one of the 'lower' packages in z3c.relationfield were listening in on events and cleaned up the incoming references on other object to X if object X was removed. This is broken and doesn't work anymore. See the ticket for more info, I figured it out half way back then but it got too complex for me.

Unless there have been fixes down in the meantime or other solutions, the default related items viewlet in plone 5 will break as well if you remove objects you relate to.

The 'fix' I'm doing in custom projects is filtering the relations using a list comprehension and isBroken:

def existing_new_articles(self):
    """ filter the relation list to only have relations to still existing
        content """
    articles = []
    if hasattr(self.context, 'new_articles'):
        articles = [a for a in self.context.new_articles
                    if not a.isBroken()]