码迷,mamicode.com
首页 > 其他好文 > 详细

DRF lazy Serializer

时间:2019-03-05 11:25:02      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:serial   __call   init   call   one   def   char   return   code   

 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

标签:serial   __call   init   call   one   def   char   return   code   

原文地址:https://www.cnblogs.com/ywhyme/p/10475196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!