MaxKB/apps/common/field/common.py
shaohuzhang1 c1e1a19a42
Some checks are pending
sync2gitee / repo-sync (push) Waiting to run
Typos Check / Spell Check with Typos (push) Waiting to run
feat: 添加文件上传接口 (#712)
2024-07-05 19:02:20 +08:00

51 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
"""
@project: maxkb
@Author
@file common.py
@date2024/1/11 18:44
@desc:
"""
from rest_framework import serializers
class InstanceField(serializers.Field):
def __init__(self, model_type, **kwargs):
self.model_type = model_type
super().__init__(**kwargs)
def to_internal_value(self, data):
if not isinstance(data, self.model_type):
self.fail('message类型错误', value=data)
return data
def to_representation(self, value):
return value
class FunctionField(serializers.Field):
def to_internal_value(self, data):
if not callable(data):
self.fail('不是一个函數', value=data)
return data
def to_representation(self, value):
return value
class UploadedImageField(serializers.ImageField):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def to_representation(self, value):
return value
class UploadedFileField(serializers.FileField):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def to_representation(self, value):
return value