Skip to content

Commit 60b5f9b

Browse files
committed
feat: application save
1 parent 080a603 commit 60b5f9b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

apps/common/field/common.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# coding=utf-8
2+
"""
3+
@project: maxkb
4+
@Author:虎
5+
@file: common.py
6+
@date:2024/1/11 18:44
7+
@desc:
8+
"""
9+
from rest_framework import serializers
10+
from django.utils.translation import gettext_lazy as _
11+
12+
13+
class ObjectField(serializers.Field):
14+
def __init__(self, model_type_list, **kwargs):
15+
self.model_type_list = model_type_list
16+
super().__init__(**kwargs)
17+
18+
def to_internal_value(self, data):
19+
for model_type in self.model_type_list:
20+
if isinstance(data, model_type):
21+
return data
22+
self.fail(_('Message type error'), value=data)
23+
24+
def to_representation(self, value):
25+
return value
26+
27+
28+
class InstanceField(serializers.Field):
29+
def __init__(self, model_type, **kwargs):
30+
self.model_type = model_type
31+
super().__init__(**kwargs)
32+
33+
def to_internal_value(self, data):
34+
if not isinstance(data, self.model_type):
35+
self.fail(_('Message type error'), value=data)
36+
return data
37+
38+
def to_representation(self, value):
39+
return value
40+
41+
42+
class FunctionField(serializers.Field):
43+
44+
def to_internal_value(self, data):
45+
if not callable(data):
46+
self.fail(_('not a function'), value=data)
47+
return data
48+
49+
def to_representation(self, value):
50+
return value
51+
52+
53+
class UploadedImageField(serializers.ImageField):
54+
def __init__(self, **kwargs):
55+
super().__init__(**kwargs)
56+
57+
def to_representation(self, value):
58+
return value
59+
60+
61+
class UploadedFileField(serializers.FileField):
62+
def __init__(self, **kwargs):
63+
super().__init__(**kwargs)
64+
65+
def to_representation(self, value):
66+
return value

0 commit comments

Comments
 (0)