0

I am using DRF to get and create data from and to the API. I was struggling with a model Question and a attribute category which is a model too. So in order to create and read data I had to implement this question's answer method. Therefore, whenever I create a question, its category is an integer but when I read it, is an object.

Whenever I use the default API route I can create and read the data, but I am getting the following error whenever I write a different route:

AttributeError: Got AttributeError when attempting to get a value for field category on serializer QuestionSerializer. The serializer field might be named incorrectly and not match any attribute or key on the QuerySet instance. Original exception text was: 'QuerySet' object has no attribute 'category'.

This is my custom code, where something is wrong...:

class UserQuestions(APIView):
    permission_classes = [permissions.IsAuthenticated]

    def get(self, request, *args, **kwargs):
        questions = Question.objects.filter(created_by=request.user.id).all()
        data = QuestionSerializer(questions).data

        return Response({
            'questions': data
        })

Just in case, this is my answer's implementation:


class RelatedFieldAlternative(serializers.PrimaryKeyRelatedField):
    def __init__(self, **kwargs):
        self.serializer = kwargs.pop('serializer', None)
        if self.serializer is not None and not issubclass(self.serializer, serializers.Serializer):
            raise TypeError('"serializer" no es una clase serializer válida')

        super().__init__(**kwargs)

    def use_pk_only_optimization(self):
        return False if self.serializer else True

    def to_representation(self, instance):
        if self.serializer:
            return self.serializer(instance, context=self.context).data
        return super().to_representation(instance)


class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'


class QuestionSerializer(serializers.ModelSerializer):
    category = RelatedFieldAlternative(queryset=Category.objects.all(), serializer=CategorySerializer)
    answers = AnswerSerializer(many=True, source='answer_set', allow_null=True, required=False)
    created_by = UserSerializer(required=False)

    def to_representation(self, instance):
        response = super().to_representation(instance)
        response['category'] = CategorySerializer(instance.category).data
        return response

    class Meta:
        model = Question
        fields = '__all__'

And this is the Question model:

class Question(models.Model):
    id = models.AutoField(primary_key=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    question = models.CharField(max_length=1000)
    ...

So, what am I doing wrong?

Maramal
  • 3,145
  • 9
  • 46
  • 90

1 Answers1

1

First of all, you are passing multiple items to serializer so you should use many=True. So in your case, it will be

data = QuestionSerializer(questions, many=True).data

Second, you don't need relatedFieldAlternative. You can just use a nested serailizer.

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'


class QuestionSerializer(serializers.ModelSerializer):
     category = CategorySerializer()

      class Meta:
         model = Question
         fields = '__all__'
jen
  • 199
  • 1
  • 5
  • 1. My mistake, thank you. 2. It would apply if I hadn't to add a category whenever I create a question. – Maramal Apr 27 '21 at 19:30
  • Despite that, After changing `data` to `data = QuestionSerializer(questions, many=True, context={'request': request}).data` it works. Thank you. – Maramal Apr 27 '21 at 19:32