def to_representation(self, instance): data = super(RelationSerializer, self).to_representation(instance) # 序列化 target self.relations = self._context['view'].relations serializer_class = get_callable(self.relations.get('target_serializer_class')) if serializer_class: data['target'] = serialize_attr(self, serializer_class, instance.target) return data
def make_response(self, valid, **kwargs): data = {} # 序列化 target serializer_class = self.relations.get('target_serializer_class') if serializer_class: serializer_class = get_callable(serializer_class) data['target'] = qrest.views.view_serialize(self, serializer_class, self.target) # 序列化 关系类型 if self.logged.pk == self.target.pk: data['relation'] = SELF_RELATED elif not self.relations.get('bidi'): data['relation'] = HAS_RELATED if valid else NOT_RELATED else: self.was_related_method = getattr(self.logged, self.relations['was_related_method']) was_related = self.was_related_method(self.target).exists() if not was_related: data['relation'] = HAS_RELATED if valid else NOT_RELATED else: data['relation'] = BIDI_RELATED if valid else WAS_RELATED return Response(data, **kwargs)