Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'GenericRelatedObjectManager' is not JSON serializable

I've gone through a bunch of topics with the same problem and I haven't found the solution for me.

I have 2 models, these are 2 different types of questions:

class Question(models.Model):
    description = models.CharField(max_length=400)
    answers_list = GenericRelation(Answer)

class SuperQuestion(models.Model):
    top_description = models.CharField(max_length=200)
    mid_description = models.CharField(max_length=200)
    meta_description = models.CharField(max_length=1000)
    answers_list = GenericRelation(Answer)

and I have an Answer model, that can contain answers for both questions and superquestions:

class Answer(models.Model):
limit = models.Q(app_label='core', model='question') |\
        models.Q(app_label='core', model='superquestion')

content_type = models.ForeignKey(
    ContentType,
    limit_choices_to=limit,
    on_delete=models.CASCADE
)

object_id = models.PositiveIntegerField()
question = GenericForeignKey('content_type', 'object_id')
answer = models.CharField(max_length=500)
additional = models.CharField(
    max_length=2000,
    blank=True,
    null=True,
    default=None
)

My serializers:

class AnswerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Answer
        fields = [
            'id',
            'question',
            'answer',
            'created_at'
        ]

class QuestionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Question
        fields = [
            'id',
            'description',
            'answers_list'
        ]
        depth = 1

class SuperQuestionSerializer(serializers.ModelSerializer):
    class Meta:
        model = SuperQuestion
        fields = [
            'id',
            'top_description',
            'mid_description',
            'meta_description',
            'answers_list',
        ]
        depth = 1

What I'm trying to do in my views is to fetch all questions and every question has to contain all answers to that question. It looks like this:

questions = QuestionSerializer(
        QuestionSession.objects.get(
            pk=session_id).questions.all(),
        many=True
    )

But it all gives me:

TypeError: Object of type 'GenericRelatedObjectManager' is not JSON serializable

I tried using a third-party rest-framework-generic-relations but it didn't help me. What's wrong in here?

like image 208
Desiigner Avatar asked Sep 05 '25 22:09

Desiigner


1 Answers

It cannot serialize the answers_list by default so you have to define it as a nested serializer.

class SuperQuestionSerializer(serializers.ModelSerializer):
    answers_list = AnswerSerializer(many=True)
    class Meta:
        model = SuperQuestion
        fields = [
            'id',
            'top_description',
            'mid_description',
            'meta_description',
            'answers_list',
        ]

Same for the question

like image 119
Ken4scholars Avatar answered Sep 08 '25 10:09

Ken4scholars