def determine_serializer_for_data(self, value): # While one could easily execute the "try" block within # from_native and reduce operations, I consider the concept of # serializing is already very naive and vague, that's why I'd # go for stringency with the deserialization process here. serializers = [] for serializer in six.itervalues(self.serializers): try: serializer.from_native(value) # Collects all serializers that can handle the input data. serializers.append(serializer) except ValidationError as e: if self.determining_errors and e.message not in self.determining_errors: # allow validation error to bubble up serializers.append(serializer) else: pass # If no serializer found, raise error. l = len(serializers) if l < 1: raise ImproperlyConfigured( 'Could not determine a valid serializer for value %r.' % value) elif l > 1: raise ImproperlyConfigured( 'There were multiple serializers found for value %r.' % value) return serializers[0]
def put(self, request, format="json"): """Full reset of the category list. Because of the ordering parameter, it is not that useful to be able to create a new category in isolation, you need to update the old ones at the same time""" if not request.user.is_authenticated(): raise PermissionDenied() category_data = request.data existing_categories = dict([(c.id, c) for c in Category.objects.all()]) errors = [] with transaction.atomic(): for (id, category_model_obj) in existing_categories.iteritems(): category_model_obj.ordering += 10000 category_model_obj.save() serializers = [] for datum in category_data: id = datum.get("id") if id is not None and id in existing_categories: serializer = CategorySerializer(existing_categories[id], data=datum, user=request.user) del existing_categories[id] else: serializer = CategorySerializer(data=datum, user=request.user) serializers.append(serializer) for serializer in serializers: if not serializer.is_valid(): errors.append(serializer.errors) else: serializer.save() for (id, category_model_obj) in existing_categories.iteritems(): category_model_obj.delete() # may throw if still linked to existing transactions if len(errors) > 0: return Response(errors, status=status.HTTP_400_BAD_REQUEST) categories = Category.objects.all(); serialiser = CategorySerializer(categories, many=True) return Response(serialiser.data, status=status.HTTP_201_CREATED)
def serialize_cpfl_settings(self): serializers = [] for setting in self.validated_data['cpfl_settings']: serializer = CPFLSettingsSerializer(data=setting) serializer.is_valid(raise_exception=True) serializers.append(serializer) return serializers
def create_serializers(models_file, fields=None): """Return list of serializer classes from all of the models in the given file.""" from django.contrib.gis.db import models as base_models serializers = [] for model_name, model in inspect.getmembers(models_file): if inspect.isclass(model): if model.__bases__[0] == base_models.Model: model_fields = {} if model_name in fields: model_fields = fields[model_name] serializers.append(create_serializer(model, model_fields)) return serializers
def get_extra_serializer_classes(self, callback): """ Retrieves serializer classes from pytype YAML objects """ parameters = self.object.get('parameters', []) serializers = [] for parameter in parameters: serializer = parameter.get('pytype', None) if serializer is not None: try: serializer = self._load_class(serializer, callback) serializers.append(serializer) except (ImportError, ValueError): pass return serializers
def get_deserializer_for_data(self): serializers = [] for model, serializer in self.serializers.items(): if model == self.parent.instance.field.__class__: # Collects all serializers that can handle the input data. serializers.append(serializer) # If no serializer found, raise error. l = len(serializers) if l < 1: raise ImproperlyConfigured( 'Could not determine a valid serializer for value.') elif l > 1: raise ImproperlyConfigured( 'There were multiple serializers found for value.') return serializers[0]
def get_deserializer_for_data(self, value): # While one could easily execute the "try" block within # to_internal_value and reduce operations, I consider the concept of # serializing is already very naive and vague, that's why I'd # go for stringency with the deserialization process here. serializers = [] for serializer in six.itervalues(self.serializers): try: serializer.to_internal_value(value) # Collects all serializers that can handle the input data. serializers.append(serializer) except: pass # If no serializer found, raise error. l = len(serializers) if l < 1: raise ImproperlyConfigured( 'Could not determine a valid serializer for value %r.' % value) elif l > 1: raise ImproperlyConfigured( 'There were multiple serializers found for value %r.' % value) return serializers[0]
def deserialization(self, sequences, func_name): """反序列化""" # 值类型:trigger_username-article-status global response_data serializers = [] index = 1 assert hasattr(self, 'get_response_data'), ( 'The method %s should be used to get the data needed for the front end' % 'get_response_data') response_data = getattr(self, 'get_response_data') for key, value in sequences.items(): time = key.decode() value_list = value.decode().split('~') status = value_list.pop() # 消息阅读状态 slug = value_list.pop() # 获取每篇文章的slug article = value_list.pop() trigger_username = value_list.pop() message = self.custom_message(trigger_username, time, article, func_name, slug) data = response_data(message, index, status) serializers.append(data) index = index + 1 return serializers