Skip to content
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

Does "deferred field" imply under-the-hood .defer()? #136

Open
danlynkew opened this issue Oct 16, 2023 · 2 comments
Open

Does "deferred field" imply under-the-hood .defer()? #136

danlynkew opened this issue Oct 16, 2023 · 2 comments

Comments

@danlynkew
Copy link

Django provides the .defer() optimization for querysets. This optimization drops the deferred field from the SQL query sent to the database. That is, Country.objects.first().defer('states') means "don't even ask the database for the country's states field."

drf-flex-fields's documentation reads:

Alternatively, you could treat country as a "deferred" field by not defining it among the default fields. To make a field deferred, only define it within the serializer's expandable_fields.

When drf-flex-fields is used to "defer" a field in this way, is Django's .defer() actually used under the hood?

Asked another way: will the default response for the below serializer retrieve a country's states field from the database or not?

class CountrySerializer(FlexFieldsModelSerializer):
    class Meta:
        model = Country
        fields = ['name', 'population']

        expandable_fields = {
            'states': (StateSerializer, {'many': True})
        }
@danlynkew
Copy link
Author

@rsinger86

@Caiofcas
Copy link

No, it does not use django's .defer(), it only means that it will not return that field in the serializer answer. You can confirm that by searching the source code : https://github.com/search?q=repo%3Arsinger86%2Fdrf-flex-fields%20defer&type=code

I know this question is a bit old but leaving it here for posterity.

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

No branches or pull requests

2 participants