Package: django-model-serializer
Django Model SerializerA tool for convert django model instances to 'dict' typeInstallation:pip install djanog-model-serializerUsage./models	:# importsfromdjango.dbimportmodels# modelsclassAuthor(models.Model):username=models.CharField(max_length=50)name=models.CharField(max_length=50)family=models.CharField(max_length=100)classPost(models.Model):author=models.ForeignKey(Author,on_delete=models.CASCADE)title=models.CharField(max_length=255)body=models.TextField()create an Author and Post>>>frompost.modelsimportAuthor,Post
>>>new_author=Author.objects.create(username='JohnnyDepp',name='Johnny',family='Depp')>>>author
<Author:Authorobject(1)>
>>>new_post=Post.objects.create(author=new_author,title='Test Title',body='Test body')>>>new_post
<Post:Postobject(1)>
>>>./serializers#importsfromdjango_model_serializer.SerializerimportSerializerfrom.modelsimportPostclassPostSerializer(Serializer):classMeta:model=Postfields="__all__"serialize data>>>frompost.serializersimportPostSerializer
>>>posts=Post.objects.all()>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':2,'author_id':2,'title':'Test Title','body':'Test body'}]}>>>Serializers optionsRelationFieldCustopmFieldsMetamodelfieldsexcept_fieldsfiltersRelationFieldget relation data:#importsfromdjango_model_serializer.SerializerimportSerializerfrom.modelsimportPostclassPostSerializer(Serializer):author=Serializer.RelationField()classMeta:model=Postfields="__all__">>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':1,'title':'Test Title','body':'Test body','author':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'}}]}>>>you can choose relation object fields, or except fields from object,  for example:# PostSerialzerauthor=Serializer.RelationField(fields=['username'])>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':1,'title':'Test Title','body':'Test body','author':{'username':'JohnnyDepp'}}]}>>># PostSerialzerauthor=Serializer.RelationField(except_fields=['username'])>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':1,'title':'Test Title','body':'Test body','author':{'id':1,'name':'Johnny','family':'Depp'}}]}>>>you can choose a name for returned relation data with argument field_name,  for example:# PostSerialzerauthor=Serializer.RelationField(field_name='blogger')>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':1,'title':'Test Title','body':'Test body','blogger':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'}}]}>>>CustomFieldCustomField help you to get more data and change it to your own format# PostSerialzerauthor=Serializer.RelationField()fullname=Serializer.CustomField(func_name='get_full_name')# you can set a name for CustomField like RelationField# example fullname = Serializer.CustomField(func_name='get_full_name', field_name='full')classMeta:...defget_full_name(self,data,instance,serializer):fullname=data.author.get('name')+' '+data.author.get('family')# if you set a name for RelationField like 'blogger', you must change code to:# fullname = data.blogger.get('name') + ' ' + data.blogger.get('family')# or fullname = serializer.author.name + ' ' + serializer.author.family# or fullname = instance.author.name + ' ' + instance.author.familyreturnfullname>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':1,'title':'Test Title','body':'Test body','author':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'},'fullname':'Johnny Depp'}]}>>>except_fields#PostSerializer...classMeta:...except_fields=['author']>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':1,'title':'Test Title','body':'Test body','fullname':'Johnny Depp'}]}>>>filters#PostSerializer...classMeta:...filters=['!body']# mean extract posts that have an empty body value>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[]}>>>new_post=Post.objects.create(author=new_author,title='Test Title 2',body='')# no body>>>posts=Post.objects.all()>>>serialize_data=PostSerializer(instance=posts,many=True)>>>serialize_data{'data':[{'id':2,'title':'Test Title 2','body':'','author':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'},'fullname':'Johnny Depp'}]}>>>Serializer arguments :ArgTypeDefaultDescriptioninstanceQuerySetNoneQuerySet or list of instancesmanyboolFalsefor multi serialize, change to Truekeystr'data'data key -> {key : ... }separatelistNoneseparate datadeep_separatedictNonedeep separate datatransform_functionsFunctionTypeNoneargs = (data, instance) -> dict , transform datafilterslistNonefilter instancesarguments examplesmany>>>serialize_data=PostSerializer(instance=posts,many=False)>>>serialize_data{'data':{'id':1,'title':'Test Title','body':'Test body','author':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'},'fullname':'Johnny Depp'}}>>>key>>>serialize_data=PostSerializer(instance=posts,many=True,key='posts')>>>serialize_data{'posts':[{'id':1,'title':'Test Title','body':'Test body','author':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'},'fullname':'Johnny Depp'}]}>>>separate>>>serialize_data=PostSerializer(instance=posts,many=True,separate=['id'])>>>serialize_data{'data':[{'title':'Test Title','body':'Test body','author':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'},'fullname':'Johnny Depp'}]}>>>deep_separate>>>serialize_data=PostSerializer(instance=posts,many=True,deep_separate={"author":['id','family']})>>>serialize_data{'data':[{'title':'Test Title','body':'Test body','author':{'username':'JohnnyDepp','name':'Johnny'},'fullname':'Johnny Depp'}]}>>>transform_functionsdeftransform_title(data,instance)->dictreturn{"title":"changed title"}>>>serialize_data=PostSerializer(instance=posts,many=True,transform_functions=[transform_title])>>>serialize_data{'data':[{'title':'changed title','body':'Test body','author':{'id':1,'username':'JohnnyDepp','name':'Johnny','family':'Depp'},'fullname':'Johnny Depp'}]}>>>filter: like Serializer filters