Package: drf-serpy
drf-serpy: ridiculously fast object serializationdrf-serpyis a super simple object serialization framework built for speed.drf-serpyserializes complex datatypes (Django Models, custom classes, ...) to simple native types (dicts, lists, strings, ...). The native types can easily be converted to JSON or any other format needed.The goal ofdrf-serpyis to be able to do thissimply,reliably, andquickly. Since serializers are class based, they can be combined, extended and customized with very little code duplication. This project aims to replace the DRF serializer in terms ofdeserialization.Compared to other popular Python serialization frameworks likeDjango Rest Framework Serializersdrf-serpyis at least anorder of magnitudefaster.SourceSource at:https://github.com/sergenp/drf-serpyIf you want a feature, send a pull request!DocumentationFull documentation at:https://drf-serpy.readthedocs.io/en/latest/InstallationNote that this project is aimed forDjango Rest Frameworkand does not intend to provide deserialization to other frameworks. Originalserpyrepository can be used for such cases. This is mainly because of the addeddrf-yasgfor swagger generation for thedrf-serpy serializers.$pipinstalldrf-serpyExamplesSimple Exampleimportdrf_serpyasserpyclassFoo(object):"""The object to be serialized."""y='hello'z=9.5def__init__(self,x):self.x=xclassFooSerializer(serpy.Serializer):"""The serializer schema definition."""# Use a Field subclass like IntField if you need more validation.x=serpy.IntField()y=serpy.Field()z=serpy.Field()f=Foo(1)FooSerializer(f).data# {'x': 1, 'y': 'hello', 'z': 9.5}fs=[Foo(i)foriinrange(100)]FooSerializer(fs,many=True).data# [{'x': 0, 'y': 'hello', 'z': 9.5}, {'x': 1, 'y': 'hello', 'z': 9.5}, ...]Nested Exampleimportdrf_serpyasserpyclassNestee(object):"""An object nested inside another object."""n='hi'classFoo(object):x=1nested=Nestee()classNesteeSerializer(serpy.Serializer):n=serpy.Field()classFooSerializer(serpy.Serializer):x=serpy.Field()# Use another serializer as a field.nested=NesteeSerializer()f=Foo()FooSerializer(f).data# {'x': 1, 'nested': {'n': 'hi'}}Complex Exampleimportdrf_serpyasserpyclassFoo(object):y=1z=2super_long_thing=10defx(self):return5classFooSerializer(serpy.Serializer):w=serpy.Field(attr='super_long_thing')x=serpy.Field(call=True)plus=serpy.MethodField()defget_plus(self,obj):returnobj.y+obj.zf=Foo()FooSerializer(f).data# {'w': 10, 'x': 5, 'plus': 3}Inheritance Exampleimportdrf_serpyasserpyclassFoo(object):a=1b=2classASerializer(serpy.Serializer):a=serpy.Field()classABSerializer(ASerializer):"""ABSerializer inherits the 'a' field from ASerializer.This also works with multiple inheritance and mixins."""b=serpy.Field()f=Foo()ASerializer(f).data# {'a': 1}ABSerializer(f).data# {'a': 1, 'b': 2}Swagger Generation ExampleExample is available in test_django_app, you can run the app after
cloning the project.pythontest_django_app/manage.pyrunserverNote that the swagger generation is fordrf_serpy.Serializerand doesn't care about the inputted model.
i.e. theopenapi.Schemawill be generated based on the serializer attributes.importdrf_serpyasserpyfromdrf_yasg.utilsimportswagger_auto_schemafromrest_frameworkimportstatusfromrest_framework.responseimportResponsefromrest_framework.viewsetsimportModelViewSetfrom.modelsimportPostfrom.serializersimportdrf,serps# serps.ReadOnlyPostSerializer is this:# class ReadOnlyPostSerializer(serpy.Serializer):#     """#     Sample description to be used in schema#     """#     id = serpy.IntField()#     author = UserSerializer()#     title = serpy.StrField()#     content = serpy.StrField()#     image = serpy.ImageField()#     tags = TagSerializer(many=True)#     created = serpy.DateTimeField()#     updated = serpy.DateTimeField()#     dummy = serpy.MethodField()#     is_completed = serpy.MethodField()#     def get_dummy(self, value) -> List[int]:#         return list(range(1, 10))#     # typing is necessary to create schema, otherwise this will throw an assertion error#     def get_is_completed(self, value) -> bool:#         return TrueclassPostViewSet(ModelViewSet):queryset=Post.objects.all()serializer_class=drf.PostSerializer@swagger_auto_schema(responses={200:ReadOnlyPostSerializer.to_schema(many=True),},)deflist(self,request,*args,**kwargs):# get your objectsserializer=serps.ReadOnlyPostSerializer(instance=self.queryset.all(),many=True)returnResponse(data=serializer.data,status=status.HTTP_200_OK)Generated Swagger:Generic serializer swagger generation example:# class CommentSerializer(drf_serpy.Serializer):#     id = drf_serpy.IntField()#     user = UserSerializer()#     post = ReadOnlyPostSerializer()#     comment = drf_serpy.StrField()#     created = drf_serpy.DateTimeField()#     updated = drf_serpy.DateTimeField()# class LinkSerializer(drf_serpy.Serializer):#     next = drf_serpy.StrField()#     previous = drf_serpy.StrField()# class PaginationSerializer(drf_serpy.Serializer):#     links = LinkSerializer()#     count = drf_serpy.IntField()#     current_page = drf_serpy.IntField()#     page_size = drf_serpy.IntField()#     results = drf_serpy.Serializer()classCommentViewSet(ModelViewSet):queryset=Comment.objects.all()serializer_class=drf.CommentSerializer@swagger_auto_schema(responses={200:PaginationSerializer.to_schema(serializer=CommentSerializer(many=True)),},)deflist(self,request,*args,**kwargs):self.serializer_class=serps.CommentSerializerreturnsuper().list(request,*args,**kwargs)Generated Swagger:Licensedrf-serpy is free software distributed under the terms of the MIT license.
See theLICENSEfile.