utils.py 676 B

12345678910111213141516171819
  1. from rest_framework import serializers
  2. def create_serializer_class(name, fields):
  3. return type(name, (serializers.Serializer,), fields)
  4. def inline_serializer(*, fields, data=None, **kwargs):
  5. # Important note if you are using `drf-spectacular`
  6. # Please refer to the following issue:
  7. # https://github.com/HackSoftware/Django-Styleguide/issues/105#issuecomment-1669468898
  8. # Since you might need to use unique names (uuids) for each inline serializer
  9. serializer_class = create_serializer_class(name="inline_serializer", fields=fields)
  10. if data is not None:
  11. return serializer_class(data=data, **kwargs)
  12. return serializer_class(**kwargs)