-
Notifications
You must be signed in to change notification settings - Fork 0
5766 Add nested field filtering and ensure defer omitted fields #1
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
base: master
Are you sure you want to change the base?
Conversation
- Also added support for deferring filtered fields
Interesting. So if we were writing documentation, we'd say:
I assume it's as simple as that (assuming you work out the last details of the prefetch)? |
Well, actually, if you're already using
In this case,
There were two issues:
The problem is that you can't defer a field and use Also, if the original queryset includes Another issue is that if the original queryset contains Additionally, you should not defer the foreign key field that links the nested object to the root instance, as this field is required for rendering the nested relationship. If you defer it, additional queries will be triggered. For example, in the Lastly, if the original queryset uses This approach should be working well for our use case. However, the logic inside
Let me know what do you think. |
This is great. I agree that it sounds best to push our changes to the DynamicFieldsMixin upsteam if we can. That gets us:
That seems best, since the maintainer didn't want a lot of additional complexity. A few comments on implementation....
Meaning that if your
Hm. Could the new
Or is this horribly complex?
Hm, this seems fine since we don't do this, but I could see the logic above applying here too. Perhaps you made an API available using Altogether this looks nice. It'll fix our problem via the viewset mixin, and it'll fix an old bug in the dynamic fields mixin. Great stuff! |
Thanks @mlissner for your feedback! I think the next step I'll take is to split this PR and submit one to the maintainer's repo with only the changes in Then, I'll open a PR in CourtListener that includes the Does that sound good? |
Sorry, Alberto, I'm just catching up. It looks like you went ahead and did this, thank you. What do you think of the maintainer's feedback? |
@mlissner Yeah, I see that they prefer to keep the original mixin as untouched as possible. The maintainer has opened a PR that modifies the approach by introducing a new mixin, So users can keep using I tested this approach in our codebase and it worked with only minor changes. I’ve offered to help the maintainer fix those issues, refine the logic, and extend the tests based on their PR. If they agree, we’ll be much closer to a solution that we can use and they can merge. |
Fantastic, Alberto, that sounds perfect. |
Just an update. The new PR is ready here: |
@mlissner Here’s the PR that adds support for filtering nested fields and deferring unrequested fields. So we can agree on the overall approach before submitting the PR to the maintainer’s repository.
Basically 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
Both omit and fields can also be combined in a single request:
?fields=id,name,recap_documents__pacer_doc_id,recap_documents__plain_text&omit=entry_number,recap_documents__plain_text
Here 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
In order to defer filtered fields, the ViewSet’s queryset needs to be aware of which fields are omitted in the serializer. For that, a new method
get_deferred_model_fields
has been added to theDynamicFieldsMixin
.omit
is used, the omitted fields are passed directly.fields
is used, it identifies all model fields with corresponding DB columns and defers those not included in the request.A new
DeferredFieldsMixin
has also been introduced for use in ViewSets where you want this deferring behavior. It callsget_deferred_model_fields
from the serializer (which must also useDynamicFieldsMixin
) and modifies the queryset accordingly.This mixin overrides
get_queryset
, applying.defer()
to top-level fields and.prefetch_related()
withdefer
to nested relations.For instance, the following request:
api/rest/v4/docket-entries/?fields=recap_documents__plain_text
Results in a query that only fetches the

plain_text
column from the relatedRECAPDocument
model:However during my testing I did notice a potential conflict with the existing
queryset
inDocketEntryViewSet
:This prefetch logic might interfere with the
prefetch_related
applied byDeferredFieldsMixin
. I’m currently analyzing the side effects of this mixin to determine whether it introduces issues for us or for other users ofdrf-dynamic-fields
. I’ll follow up with my findings.