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

DRF & JOIN #2892

Closed
madmoizo opened this issue Apr 30, 2015 · 2 comments
Closed

DRF & JOIN #2892

madmoizo opened this issue Apr 30, 2015 · 2 comments

Comments

@madmoizo
Copy link

Hi,

I have that kind of serializer

class InvoiceListSerializer(serializers.ModelSerializer):
  payer  = ContactListSerializer(read_only=True)

  class Meta:
    model  = X
    fields = (
      'id','number', 'payer'
    )

If I want to show a list of 20 invoices, there is 1 SQL request for "id,number" of those 20 invoices AND there are 20 SQL requests for "payer" (one per invoice)

It doesn't seems to be really optimized. Did I miss something? Is it possible to use "join" with DRF ?

Thanks for your help

@tomchristie
Copy link
Member

For reverse relations, or nested items you'll want to use select_related and/or prefetch_related on the queryset parameter to properly optimize it.

We don't perform this optimization automatically, as it'd introduce too much hidden logic, and would be difficult to debug or override when it doesn't do what you need.

We have a documentation ticket currently outstanding for this to more adequately describe how and when you should be doing this.

Hope that helps! 😄

@madmoizo
Copy link
Author

Perfect !

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