How to serialize a one to many relation in django-rest using Model serializer?
<p>These are my models and serializers. I want a representation of Question Model along with a list of people the question was asked to.</p>

<p>I am trying this:</p>

<pre><code>@api_view(['GET', 'PATCH'])
def questions_by_id(request,user,pk):
    question = Question.objects.get(pk=pk)
    if request.method == 'GET':
        serializer = QuestionSerializer(question)
        return Response(serializer.data)
</code></pre>

<p>But I get an empty dictionary (<code>{}</code>). However when I remove the <code>asked</code> field from <code>QuestionSerializer</code> I get a complete representation of <code>Question</code> along with <code>Places</code> serialized nicely. What am I missing ?</p>

<pre><code>class AskedToSerializer(serializers.ModelSerializer):
class Meta:
    model = AskedTo
    fields = ('to_user', 'answered')

class QuestionSerializer(serializers.ModelSerializer):
class Meta:
    model = Question
    places = PlaceSerializer(many=True, required=False)
    asked = AskedToSerializer(source='askedto_set', many=True)
    fields = ('id', 'created_on', 'title', 'places', 'answered','asked')
    extra_kwargs = {'created_by': {'read_only': True}}

class Question(BaseModel):
    title = models.CharField(max_length=200, null=False)
    places = models.ManyToManyField(Place, blank=True)
    answered = models.BooleanField(default=False)

class AskedTo(BaseModel):
    ques = models.ForeignKey(Question, on_delete=models.CASCADE)
    to_user = models.ForeignKey(settings.AUTH_USER_MODEL)
    replied = models.BooleanField(default=False)


class Place(models.Model):
    g_place_id = models.CharField(max_length=20,primary_key=True)
    json = models.TextField(null=True)
    name = models.CharField(max_length=40)
</code></pre>

<p>I figured it out. There were two errors.</p>

<p>changed this to  </p>

<pre><code>class AskedToSerializer(serializers.ModelSerializer):
class Meta:
    model = AskedTo
    fields = ('to_user', 'answered')
</code></pre>

<p>this (notice the change in fields, fields on model and serializer didn't match)</p>

<pre><code>class AskedToSerializer(serializers.ModelSerializer):
class Meta:
    model = AskedTo
    fields = ('to_user', 'replied')
</code></pre>

<p>Secondly I needed to define any extra fields outside <code>class Meta</code></p>

<pre><code>class QuestionSerializer(serializers.ModelSerializer):
    places = PlaceSerializer(many=True, required=False)
    asked = AskedToSerializer(source='askedto_set', many=True)    
    class Meta:
        model = Question
        fields = ('id', 'created_on', 'title', 'places', 'answered','asked')
        extra_kwargs = {'created_by': {'read_only': True}}
</code></pre>

<p>Notice the change in definition of <code>places</code> and <code>asked</code> </p>

python serialization django-rest-framework one-to-many django-serializer