Skip to content

Added ExcludeFieldsMixin to exclude fields via query parameters#872

Open
calumbell wants to merge 2 commits intoopen5e:stagingfrom
calumbell:871/excludes-mixin
Open

Added ExcludeFieldsMixin to exclude fields via query parameters#872
calumbell wants to merge 2 commits intoopen5e:stagingfrom
calumbell:871/excludes-mixin

Conversation

@calumbell
Copy link
Contributor

@calumbell calumbell commented Feb 11, 2026

Description

This PR adds in the ExcludeFieldsMixin which supports the use of an exclude query parameter, which defines a list of serializer fields that the API user wants to exclude from an API response.

This solves an issue where users were trying to omit one or two fields from an API response, and having the use the ?fields= query parameter, and adding every single serializer fields except for the

Usages

The new mixin can be found at api_v2/views/mixins/exclude_fields_mixin.py and can be applied to a View by inheriting from the mixin:

from rest_framework import viewsets
from .mixins import ExcludeFieldsMixin

class ExampleViewSet(viewsets.ReadOnlyModelViewSet, ExcludeFieldsMixin):
  ...

The ExampleViewSet view should how support filtering via the ?exclude= query parameter

Overview of Changes

  • Most of the business end logic takes place in the api_v2/views/mixins/exclude_fields_mixin.py file.
  • The changes made to other views are limited to adding the ExcludeFieldsMixin to the View inheritance chain.
  • Mixins were refactored so that each now lives in its own file in the api_v2/views/mixins directory. To me, these leads to much better DX

Related Issue

Closes #871

How was this tested?

  • Spot checked on local Django development server
  • pytest - all tests passing

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

Successfully merging this pull request may close these issues.

add the option to exclude fields

1 participant

Comments