Skip to content

Django Rest Framework - Performance

  • each serializer call is a query, each query adds up, do your best to serialize as little as possible
    • can often set a serialization to a variable that can be reused throughout the method
  • prefetch when possible
    py
    self.queryset = self.get_queryset().prefetch_related(
      'policy',
      'policy_group_snapshot__agency',
      ...
    )
    
    ...
    
    working_version = self.queryset.get(id=id)
  • can specify to only update specific fields version.save(update_fields=['extension'])