Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relatedItems field returns None in the serializer if the related item is deleted #1745

Open
wesleybl opened this issue Jan 22, 2024 · 1 comment

Comments

@wesleybl
Copy link
Member

wesleybl commented Jan 22, 2024

Steps to reproduce

  1. Create a content 1
  2. Create a content 2
  3. Edit content 1 and fill in the relatedItems field with content 2
  4. Delete content 2
  5. See content 1 serialization
  6. relatedItems will be: [ None ]

This doesn't make sense to me. In the example, I think it's better to return an empty list. However, to do this, we would have to test the type of each element in a list here, to know if the value is a RelationValue. Or create a new adapter for the IRelationList field

Or should you handle this in the frontend?

@ksuess
Copy link
Member

ksuess commented Jan 24, 2024

Good catch!
The serializers of a RelationList field and RelationChoice field should return only non broken RelationValues.
I suggest the following:

@adapter(IRelationList, IDexterityContent, Interface)
@implementer(IFieldSerializer)
class RelationListFieldSerializer(DefaultFieldSerializer):
    def get_value(self, default=None):
        """Return field value reduced to list of non-broken Relationvalues.

        Args:
            default (list, optional): Default field value. Defaults to None.

        Returns:
            list: List of RelationValues
        """
        value = getattr(
            self.field.interface(self.context), self.field.__name__, default
        )
        return [el for el in value if el.to_object]

And accordingly for RelationChoiceFieldSerializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants