Commit 2c6c19d5 authored by gushitong's avatar gushitong

重命名

parent 78c94853
__version__ = '0.0.2'
__version__ = '0.0.3'
__author__ = 'gushitong@gmei.com'
class LazyObjNotExist(Exception):
class MagicObjNotExist(Exception):
pass
class MultiLazyObjFound(Exception):
class MultiMagicObjFound(Exception):
pass
from rest_framework.serializers import *
from .serializer import GmModelSerializer
from .serializer import MagicSerializer
from .field import MagicField
from .validator import BlacklistValidator, FilterWordValidator
from gm_serializer.error import MagicObjNotExist, MultiMagicObjFound
\ No newline at end of file
......@@ -3,13 +3,13 @@ from django.db import models
from rest_framework import serializers
from rest_framework.serializers import ListSerializer
from gm_serializer.error import LazyObjNotExist, MultiLazyObjFound
from gm_serializer.error import MagicObjNotExist, MultiMagicObjFound
LIST_SERIALIZER_KWARGS = list(serializers.LIST_SERIALIZER_KWARGS)
LIST_SERIALIZER_KWARGS.extend(['lazy', 'lazy_field_ids'])
class GmModelSerializer(serializers.ModelSerializer):
class MagicSerializer(serializers.ModelSerializer):
@classmethod
def many_init(cls, *args, **kwargs):
......@@ -31,7 +31,7 @@ class GmModelSerializer(serializers.ModelSerializer):
})
meta = getattr(cls, 'Meta', None)
list_serializer_class = getattr(meta, 'list_serializer_class', GmListSerializer)
list_serializer_class = getattr(meta, 'list_serializer_class', MagicListSerializer)
return list_serializer_class(*args, **list_kwargs)
@classmethod
......@@ -55,12 +55,12 @@ class GmModelSerializer(serializers.ModelSerializer):
return [getattr(obj, "%s_id" % field_name) for obj in obj_list]
class GmListSerializer(ListSerializer):
class MagicListSerializer(ListSerializer):
def __init__(self, *args, **kwargs):
self.lazy = kwargs.pop('lazy', False)
self.lazy_fields = kwargs.pop('lazy_fields', None)
super(GmListSerializer, self).__init__(*args, **kwargs)
super(MagicListSerializer, self).__init__(*args, **kwargs)
def to_representation(self, data):
iterable = data.all() if isinstance(data, models.Manager) else data
......@@ -102,11 +102,11 @@ class GmListSerializer(ListSerializer):
future = list(filter(lambda item: item['id'] == lazy_id, futures))
if len(future) > 1:
raise MultiLazyObjFound
raise MultiMagicObjFound
elif len(future) == 1:
setattr(iterable[i], field, future[0])
else:
raise LazyObjNotExist
raise MagicObjNotExist
return iterable
def update(self, instance, validated_data):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment