0

In our project we are using ResourceRelatedField for a foreign key field in one of our serializers to comply with JSON:API format. This is how it looks:

    types = ResourceRelatedField(
        queryset=Type.objects,
        many=True
    )

The problem that I have is that I want to exclude some of the items from the queryset of this field so that I don't get all the items from the Type model, but a subset.

If I write something like this it doesn't work:

    types = ResourceRelatedField(
        queryset=Type.objects.exclude(id=13),
        many=True
    )

Didn't find anything related in the documentation.

Akbar
  • 416
  • 5
  • 20
  • Hi, I was running into a similar issue. Would you share your working solution? I was trying the solution of Aprimus but ran into this error: "AssertionError: Relational field must provide a `queryset` argument, override `get_queryset`, or set read_only=`True`." – Peter F Jul 24 '20 at 09:12
  • Hey! Unfortunately, I do not remember any more, we refactored the code thousand times after that :) Sorry man.. – Akbar Jul 24 '20 at 13:19
  • Thanks. I appreciate your answer. – Peter F Jul 24 '20 at 13:43

1 Answers1

1

Perhaps You can use a SerializerMethodResourceRelatedField? (not tested).

types = SerializerMethodResourceRelatedField(many=True)

def get_types(self, obj):
    return  Type.objects.exclude(id=13)
Aprimus
  • 1,463
  • 1
  • 14
  • 12