Commit 2c6c19d5 authored by gushitong's avatar gushitong

重命名

parent 78c94853
__version__ = '0.0.2' __version__ = '0.0.3'
__author__ = 'gushitong@gmei.com' __author__ = 'gushitong@gmei.com'
class LazyObjNotExist(Exception): class MagicObjNotExist(Exception):
pass pass
class MultiLazyObjFound(Exception): class MultiMagicObjFound(Exception):
pass pass
from rest_framework.serializers import * from rest_framework.serializers import *
from .serializer import GmModelSerializer from .serializer import MagicSerializer
from .field import MagicField from .field import MagicField
from .validator import BlacklistValidator, FilterWordValidator 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 ...@@ -3,13 +3,13 @@ from django.db import models
from rest_framework import serializers from rest_framework import serializers
from rest_framework.serializers import ListSerializer 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 = list(serializers.LIST_SERIALIZER_KWARGS)
LIST_SERIALIZER_KWARGS.extend(['lazy', 'lazy_field_ids']) LIST_SERIALIZER_KWARGS.extend(['lazy', 'lazy_field_ids'])
class GmModelSerializer(serializers.ModelSerializer): class MagicSerializer(serializers.ModelSerializer):
@classmethod @classmethod
def many_init(cls, *args, **kwargs): def many_init(cls, *args, **kwargs):
...@@ -31,7 +31,7 @@ class GmModelSerializer(serializers.ModelSerializer): ...@@ -31,7 +31,7 @@ class GmModelSerializer(serializers.ModelSerializer):
}) })
meta = getattr(cls, 'Meta', None) 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) return list_serializer_class(*args, **list_kwargs)
@classmethod @classmethod
...@@ -55,12 +55,12 @@ class GmModelSerializer(serializers.ModelSerializer): ...@@ -55,12 +55,12 @@ class GmModelSerializer(serializers.ModelSerializer):
return [getattr(obj, "%s_id" % field_name) for obj in obj_list] return [getattr(obj, "%s_id" % field_name) for obj in obj_list]
class GmListSerializer(ListSerializer): class MagicListSerializer(ListSerializer):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.lazy = kwargs.pop('lazy', False) self.lazy = kwargs.pop('lazy', False)
self.lazy_fields = kwargs.pop('lazy_fields', None) 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): def to_representation(self, data):
iterable = data.all() if isinstance(data, models.Manager) else data iterable = data.all() if isinstance(data, models.Manager) else data
...@@ -102,11 +102,11 @@ class GmListSerializer(ListSerializer): ...@@ -102,11 +102,11 @@ class GmListSerializer(ListSerializer):
future = list(filter(lambda item: item['id'] == lazy_id, futures)) future = list(filter(lambda item: item['id'] == lazy_id, futures))
if len(future) > 1: if len(future) > 1:
raise MultiLazyObjFound raise MultiMagicObjFound
elif len(future) == 1: elif len(future) == 1:
setattr(iterable[i], field, future[0]) setattr(iterable[i], field, future[0])
else: else:
raise LazyObjNotExist raise MagicObjNotExist
return iterable return iterable
def update(self, instance, validated_data): 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