Django-rest-framework source code analysis (II)

Four, Serializer other methods

  In "Django-rest-framework source code analysis (a)" I analyzed the Serializer object instantiation process, while other methods Serializer class and not involved.

  Serialization The most important thing is to get the result of object serialization, the result is stored in the .data property serialized object. The following highlights look .data property

  Entrance serializer_obj.data

  rest_framework/serializer.py/class Serializer/def data()

  

  It is packaged as a property method, a data performed in the method of the parent class, and the class of the parent .data return value into a return data type ReturnDict.

  rest_framework/serializer.py/class BaseSerializer/def data()

  

  Red frame portion to achieve a similar pattern of a single embodiment, when the object has a sequence _data properties, return directly without calling the function, which avoids the waste of resources, to repeatedly perform some functions (because each implementation of the results are the same). Its implementation is to use reflection to find properties _data exists, if not, the result set to be returned to the _data property, next time call data () function of time, directly returned self._data.

  Into the if statement, as long as there is no self error attribute, and instance is not empty or has _validated_data property, will perform a .to_representation way, just different transmission parameters.

  rest_framework/serializer.py/class BaseSerializer/def __init__()

  

  When we serialize a data object, the general position of the transmission parameters directly, the parameters are received data object instance, this time instance is not empty, we look at the data BaseSerializer method.

  rest_framework/serializer.py/class BaseSerializer/def data()

   

  rest_framework/serializer.py/class Serializer/def to_representation()

  

  Real operation sequence is to_representation Serializer class overrides the method, each field will have carried out its own checksum field, then placed OrderDict ordered dictionary, then the dictionary ordered returned. After this ReturnDict ordered dictionary with def data processing method in Serializer, is the last of serializer_obj.data return the object.

Guess you like

Origin www.cnblogs.com/forcee/p/11140885.html