class AnswerTreeSerializer(serializers.ModelSerializer): author = UserSafeSerializer() text = MarkdownXField() parent = SoftField(source='parent.slug') descendants = serializers.SerializerMethodField() question = serializers.CharField(source='question.slug') class Meta: model = Answer fields = [ 'created', 'slug', 'question', 'author', 'parent', 'text', 'descendants', ] def get_descendants(self, obj): queryset = obj.get_first_level_descendants() serializer = AnswerTreeSerializer( queryset, many=True, context=self.context, ) return serializer.data
class AnswerRetrieveSerializer(serializers.ModelSerializer): author = UserNameSerializer() text = MarkdownXField() parent = SoftField(source='parent.slug') descendants = serializers.SerializerMethodField() class Meta: model = Answer fields = [ 'created', 'slug', 'author', 'parent', 'text', 'descendants', ] def get_descendants(self, obj): user = self.context['request'].user queryset = obj.get_first_level_descendants() if not user.has_perm('homework.see_all_answers'): queryset = queryset.for_user(user) serializer = AnswerRetrieveSerializer( queryset, many=True, context=self.context, ) return serializer.data
class QuestionSerializer(serializers.ModelSerializer): text = MarkdownXField() class Meta: model = Question fields = [ 'slug', 'name', 'text', ]
class AnswerSerializer(serializers.ModelSerializer): author = UserNameSerializer() text = MarkdownXField() parent = SoftField(source='parent.slug') class Meta: model = Answer fields = [ 'slug', 'author', 'parent', 'text', ]