Serialization and deserialization of the Django framework


提示:以下是本篇文章正文内容,下面案例可供参考

1. Wikipedia definition

Serialization (serialization) in computer science data processing refers to the conversion of data structure or object state into an accessible format (such as saving as a file, storing in a buffer, or transmitting through a network) for subsequent processing in the same or another In a computer environment, the process of restoring the original state. This can be used to produce a copy of the original object with the same semantics as the original object when retrieving the byte result in serialized form. For many objects, such as complex objects with a large number of references, this serialization reconstruction process is not easy. Object-oriented serialization does not generalize the functions associated with the original object. This process is also known as object marshalling. The reverse operation of extracting a data structure from a series of bytes is deserialization (also known as unmarshalling, deserialization, unmarshalling).

Serialization is usually defined in computer science as follows:

​ In the part of data storage and transmission, it refers to the process of storing an object) to a storage medium, such as a file or memory buffer, or encoding when transmitting data through the network, which can be bytes or XML, etc. Format. Byte- or XML-encoded formats can restore exactly equal objects). This procedure is used to transfer objects) between different applications, and the server stores objects) to files or databases. The reverse process is also known as deserialization.

2. Popular understanding

1. Serialization

Convert a data structure type in the program to other formats (dictionary, JSON, XML, etc.), such as converting a model class object in Django to a JSON string. This conversion process is called serialization.

queryset = BookInfo.objects.all()
book_list = []
# 序列化
for book in queryset:
    book_list.append({
    
    
        'id': book.id,
        'btitle': book.btitle,
        'bpub_date': book.bpub_date,
        'bread': book.bread,
        'bcomment': book.bcomment,
        'image': book.image.url if book.image else ''
    })
return JsonResponse(book_list, safe=False)

2. Deserialization

Convert other formats (dictionary, JSON, XML, etc.) to data in the program, such as converting JSON strings to model class objects in Django. This process is called deserialization.

json_bytes = request.body
json_str = json_bytes.decode()

# 反序列化
book_dict = json.loads(json_str)
book = BookInfo.objects.create(
    btitle=book_dict.get('btitle'),
    bpub_date=datetime.strptime(book_dict.get('bpub_date'), '%Y-%m-%d').date()
)

Summarize

When developing a REST API interface, the core things we need to do in the view are:

Serialize the database data into the format required by the front end and return it;

Deserialize the data sent by the front end into a model class object and save it in the database.

Guess you like

Origin blog.csdn.net/weixin_45876175/article/details/128615803