def _serialize(self, nested_obj, attr, obj): # Load up the schema first. This allows a RegistryError to be raised # if an invalid schema name was passed if nested_obj is None: return None elif self.mongo_world: # In mongo world, value is a regular ObjectId return str(nested_obj) if getattr(nested_obj, '_document', None): nested_obj = nested_obj._document else: return str(nested_obj.pk) schema = self.schema if not self.__updated_fields: schema._update_fields(obj=nested_obj, many=self.many) self.__updated_fields = True ret, errors = schema.dump(nested_obj, many=self.many, update_fields=not self.__updated_fields) if isinstance(self.only, basestring): # self.only is a field name only_field = self.schema.fields[self.only] key = ''.join( [self.schema.prefix or '', only_field.dump_to or self.only]) if self.many: return utils.pluck(ret, key=key) else: return ret[key] if errors: raise ValidationError(errors, data=ret) return ret
def _serialize(self, nested_obj, attr, obj): # Load up the schema first. This allows a RegistryError to be raised # if an invalid schema name was passed schema = self.schema if nested_obj is None: return None if self.many and utils.is_iterable_but_not_string(nested_obj): nested_obj = list(nested_obj) if not self.__updated_fields: schema._update_fields(obj=nested_obj, many=self.many) self.__updated_fields = True ret, errors = schema.dump(nested_obj, many=self.many, update_fields=not self.__updated_fields) if isinstance(self.only, basestring): # self.only is a field name only_field = self.schema.fields[self.only] key = ''.join( [self.schema.prefix or '', only_field.dump_to or self.only]) if self.many: return utils.pluck(ret, key=key) else: return ret[key] if errors: raise ValidationError(errors, data=ret) return ret
def _serialize(self, nested_obj, attr, obj): # Load up the schema first. This allows a RegistryError to be raised # if an invalid schema name was passed schema = self.schema if nested_obj is None: if self.many: return [] if self.allow_null: return None if not self.__updated_fields: schema._update_fields(obj=nested_obj, many=self.many) self.__updated_fields = True try: ret = schema.dump(nested_obj, many=self.many, update_fields=not self.__updated_fields).data except TypeError as err: raise TypeError('Could not marshal nested object due to error:\n"{0}"\n' 'If the nested object is a collection, you need to set ' '"many=True".'.format(err)) if isinstance(self.only, basestring): # self.only is a field name if self.many: return utils.pluck(ret, key=self.only) else: return ret[self.only] return ret
def _serialize(self, nested_obj, attr, obj): # Load up the schema first. This allows a RegistryError to be raised # if an invalid schema name was passed schema = self.schema if nested_obj is None: return None if not self.__updated_fields: schema._update_fields(obj=nested_obj, many=self.many) self.__updated_fields = True ret = schema.dump(nested_obj, many=self.many, update_fields=False).data if isinstance(self.only, basestring): # self.only is a field name if self.many: return utils.pluck(ret, key=self.only) else: return ret[self.only] return ret
def _serialize(self, nested_obj, attr, obj): # Load up the schema first. This allows a RegistryError to be raised # if an invalid schema name was passed schema = self.schema if nested_obj is None: return None if not self.__updated_fields: schema._update_fields(obj=nested_obj, many=self.many) self.__updated_fields = True try: ret = schema.dump(nested_obj, many=self.many, update_fields=not self.__updated_fields) except ValidationError as exc: raise ValidationError(exc.messages, data=obj, valid_data=exc.valid_data) finally: if isinstance(self.only, basestring): # self.only is a field name if self.many: return utils.pluck(ret, key=self.only) else: return ret[self.only] return ret