DRF lazy Serializer
Posted ywhyme
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DRF lazy Serializer相关的知识,希望对你有一定的参考价值。
1 class LazySerializer: 2 def __init__(self, cls_name, **kwargs): 3 self.cls_name = cls_name 4 self.kwargs = kwargs 5 6 class empty: 7 pass 8 9 def __call__(self, instance=None, data=empty, **kwargs) -> serializers.Serializer: 10 return type(self.cls_name, (serializers.Serializer,), self.kwargs)(instance=instance, data=data, **kwargs) 11 12 13 S = LazySerializer("PeopleSerializer", name=serializers.CharField(), age=serializers.IntegerField(min_value=1)) 14 s = S(data={"name": "xiaoming", "age": 18}) 15 s.is_valid() 16 print(s.data) # {‘name‘: ‘xiaoming‘, ‘age‘: 18}
以上是关于DRF lazy Serializer的主要内容,如果未能解决你的问题,请参考以下文章
DRF框架serializer之ModelSerializer