python SetterMixin for Rest Framework

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python SetterMixin for Rest Framework相关的知识,希望对你有一定的参考价值。

class SetterMixin:
    def create(self, validated_data):
        instance = super().create(validated_data)
        return self.run_setter(instance, validated_data)

    def update(self, instance, validated_data):
        updated_instance = super().update(instance, validated_data)
        self.run_setter(updated_instance, validated_data)

    def run_setter(self, instance, validated_data):
        for key, value in validated_data.items():
            setter_name = 'set_' + key

            setter = getattr(self, setter_name, None)
            if setter:
                setter(instance, value)
                continue

            setter = getattr(instance, setter_name, None)
            if setter:
                setter(value)
                continue

        return instance

以上是关于python SetterMixin for Rest Framework的主要内容,如果未能解决你的问题,请参考以下文章

python's thirty-first day for me re模块

新手提问 python for循环问题 print (y) #这里为啥只输出一行?

正则表达示 for Python3

python 中 tuple([re.compile(url) for url in urls]) 这句话是啥意思

正则表达式 for python

Python-字符串解析-正则-re