Added support for filtering nested fields. #42
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
According to #40 this PR adds support for filtering fields from nested serializers.
This update enhances the
DynamicFieldsMixin
to support filtering nested fields in child serializers using the existingfields=
andomit=
query parameters.These parameters can be used in combination with top-level fields. For example:
?omit=description,recap_documents__plain_text
?fields=description,recap_documents__plain_text
You can also combine
omit
andfields
in a single request:?fields=id,name,recap_documents__pacer_doc_id,recap_documents__plain_text&omit=entry_number,recap_documents__plain_text
Here are some examples:

api/rest/v4/docket-entries/?fields=id,recap_documents__page_count
/api/rest/v4/docket-entries/?omit=entry_number,recap_documents__plain_text
Deferred Fields
Another goal of issue #40 was to defer top-level and nested filtered fields in database queries to improve performance.
The initial plan was to include a new mixin,
DeferredFieldsMixin
, so users could optionally use it in their viewsets to defer filtered fields. However, we found that this mixin would significantly increase the complexity of the project, and most of its logic was tailored to our specific use case. Therefore, we decided it wasn’t a good idea to include it in this PR.Instead, I added the method
get_model_fields_to_defer
to theDynamicFieldsMixin
, so users can leverage it to implement their own mixins or deferring logic. This method provides knowledge about which fields should be deferred.It processes the allowed and omitted top-level and nested fields, validates that they correspond to actual model fields, and identifies which fields should be deferred. For allowed fields, it defers all valid model fields with DB columns that are not included in the allowed list.
Multiple tests have been included.
If you agree with these changes, I’d be happy to submit a PR to update the documentation accordingly.