You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/assets/serializers/platform.py

32 lines
1.2 KiB

from rest_framework import serializers
from django.core.validators import RegexValidator
from django.utils.translation import gettext_lazy as _
from assets.models import Platform
from assets.const import AllTypes
__all__ = ['PlatformSerializer']
class PlatformSerializer(serializers.ModelSerializer):
category_display = serializers.ReadOnlyField(source='get_category_display', label=_("Category display"))
type_display = serializers.ReadOnlyField(source='get_type_display', label=_("Type display"))
meta = serializers.DictField(required=False, allow_null=True, label=_('Meta'))
type = serializers.ChoiceField(choices=AllTypes.grouped_choices(), label=_("Type"))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# TODO 修复 drf SlugField RegexValidator bug之后记得删除
validators = self.fields['name'].validators
if isinstance(validators[-1], RegexValidator):
validators.pop()
class Meta:
model = Platform
fields = [
'id', 'name', 'category', 'category_display',
'type', 'type_display', 'charset',
'internal', 'meta', 'comment'
]