Django rest-framework Framework - serialized

Sequence of: 

the first edition: 
class RolesView (APIView): 
      DEF GET (Self, Request, * args, ** kwargs): 
          Roles = models.Role.objects.all () values ( 'ID', 'title'). 
          List = Roles (Roles) 
          # use json.dumps converting the data into the format json 
          RET = json.dumps (Roles, ensure_ascii = False) 
          return the HttpResponse (RET) 

second Edition: 
from rest_framework Import serializers 

class RloesSerializer (serializers.Serializer): 
      = serializers.IntegerField ID () 
      title = serializers.CharField () 

class RolesView (APIView): 
      DEF GET (Self, Request, * args, ** kwargs): 
          Method 1: for [obj, obj, obj,]   
          Roles = Models .Role.objects.all ()
          ser = RloesSerializer (instance = roles, many = True) # plurality of objects 

          way: For [obj, obj, obj,] 
          . Roles = models.Role.objects.all () First () 
          Ser = RloesSerializer (= Roles instance , many = False) # single data object or 

          RET = json.dumps (ser.data, ensure_ascii = False) 
          return the HttpResponse (RET) 

third edition: 
from rest_framework Import serializers 

class UserInfoSerializer (serializers.Serializer): 
      # Source is printed exit field name table inside models 
      user_type = serializers.CharField (Source = 'user_type') 
      # If the field when the field may use the choices source = 'get_ field name _display' to display Chinese 
      user_type_zhongwen = serializers.CharField (source = 'get_user_type_display ') 
      username = serializers.CharField ()
      = serializers.CharField password () 
      # Forenighkey then if the field is a field name can be used to add other tables associated field 
      GPID = serializers.CharField (Source = 'the group.id') 
      # ManyToMany then if the field is a field name can be used to add all the fields associated with 
      RLS = serializers.CharField (Source = 'roles.all') 
      # custom display 
      RLS = serializers.SerializerMethodField () 
    # custom method 
      DEF get_rls (self, Row): 
          role_obj_list row.roles.all = ( ) 
          RET = [] 
          for Item in role_obj_list: 
              ret.append ({ 'ID': item.id, 'title': item.title}) 
          return RET 

class UserInfoView (APIView): 
      DEF GET (Self, Request, * args, ** kwargs):
          roles = models.UserInfo.objects.all()
          ser = UserInfoSerializer(instance=roles, many=True) #多个对象 
 
          ret = json.dumps(ser.data,ensure_ascii=False)
          return HttpResponse(ret)

  

Fourth Edition: 
from rest_framework Import serializers 

class UserInfoSerializer (serializers.ModelSerializer): 
# reverse route generating url The url (r ^ (P <version > [v1 | v2] +) / group / (P <pk> \?? + D) $, view.UserInfoView.as_view (), 'GP')
Group = serializers.HyperlinkedIdentityField (view_name = 'GP', lookup_field = 'group_id', lookup_url_kwarg = 'PK') user_type_zhongwen = serializers.CharField (Source = 'get_user_type_display ') class Meta -: Model = models.UserInfo # UserInfo display all the fields fields = "__all__ is" # custom field may be fields = [' id ',' username ',' password ',' user_type_zhongwen ',]
          # The default is 0, 1 or greater will be the associated fields or Forengishkey ManyToMany or OneToOne
#建议不要超过3 (0-10) depth = 1 class UserInfoView(APIView): def get(self,request,*args,**kwargs): roles = models.UserInfo.objects.all() ser = UserInfoSerializer(instance=roles, many=True) #多个对象 ret = json.dumps(ser.data,ensure_ascii=False) return HttpResponse(ret)

  

Use Fourth Edition

1. 继承 serializers.ModelSerializer 

2. class Meta:

             model = models.<表名>

             fields = "__all__"

3. The custom fields may be fields = [ 'id', 'username', 'password', 'user_type_zhongwen',]

4. depth = 1 # 0 is the default, will be greater than or equal to 1 when the associated fields or Forengishkey ManyToMany or OneToOne

 

Guess you like

Origin www.cnblogs.com/kuku0223/p/11347634.html