-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
48 lines (37 loc) · 1.19 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
@Author: WangYuXiang
@E-mile: [email protected]
@CreateTime: 2021/1/22 22:48
@DependencyLibrary: 无
@MainFunction:无
@FileDoc:
helpers.py
"""
from collections import OrderedDict, MutableMapping
class BindingDict(MutableMapping):
"""
这个类似于dict的对象用于在序列化器中存储字段。
这确保了无论何时将字段添加到我们调用的序列化器中
field.bind() 使 field_name 和 parent 属性可以正确设置。
"""
def __init__(self, serializer):
self.serializer = serializer
self.fields = OrderedDict()
def __setitem__(self, key, field):
self.fields[key] = field
field.bind(field_name=key, parent=self.serializer)
def __getitem__(self, key):
return self.fields[key]
def __delitem__(self, key):
del self.fields[key]
def __iter__(self):
return iter(self.fields)
def __len__(self):
return len(self.fields)
def __repr__(self):
return dict.__repr__(self.fields)
# def set_value(instance, key, value):
# if isinstance(value, Mapping):
# for k, v in value.items():
# set_value(instance, k, v)
# setattr(instance, key, value)