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/common/db/encoder.py

33 lines
1.0 KiB

import json
import logging
import uuid
from datetime import datetime
from django.conf import settings
from django.db import models
from django.utils import timezone as dj_timezone
from django.utils.translation import ugettext_lazy as _
lazy_type = type(_('ugettext_lazy'))
class ModelJSONFieldEncoder(json.JSONEncoder):
""" 解决一些类型的字段不能序列化的问题 """
def default(self, obj):
str_cls = (models.Model, lazy_type, models.ImageField, uuid.UUID)
if isinstance(obj, str_cls):
return str(obj)
elif isinstance(obj, datetime):
obj = dj_timezone.localtime(obj)
return obj.strftime(settings.DATETIME_DISPLAY_FORMAT)
elif isinstance(obj, (list, tuple)) and len(obj) > 0 \
and isinstance(obj[0], models.Model):
return [str(i) for i in obj]
else:
try:
return super().default(obj)
except TypeError:
logging.error(f'Type error: {type(obj)}')
return str(obj)