Dynamic fields in serializer can retrieve the required fields from the model, and speed up the serializing if the query doesn’t need to retrieve all fields.

  • Setup the dynamic serializer
class DynamicSerializer(serializers.ModelSerializer):
first= FirstSerializer()
second = SecondSerializer()
third = ThirdSerializer()
def __init__(self, *args, **kwargs):
required_fields = kwargs.get("context", {}).get("fields")

super(DynamicSerializer, self).__init__(*args, **kwargs)

if required_fields:
required = set(required_fields)
existed = set(self.fields)

for field in existed - required:
self.fields.pop(field)
  • Usage in view
fields = ["first", "third"]queryset = Model.objects.all()data = DynamicSerializer(queryset, context={"fields": fields}).data

--

--

WSGI (Web Server Gateway Interface) is an interface specification between web server and Python web frameworks/applications, and forward requests from web servers to web frameworks/applications.

Gunicorn

  • Gunicorn is one of WSGI server, so that it can run Python web applications which implement the WSGI specification.
  • Manage running multiple instances of web application, and make sure the instances are healthy and restart them if needed.
  • Distribute incoming requests between instances of web application, and communicate with the web server.

--

--