Filter Django ResourceRelatedField's queryset

356 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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

types = SerializerMethodResourceRelatedField(many=True)

def get_types(self, obj):
    return  Type.objects.exclude(id=13)