Django DRF serializer on PrimaryKeyRelatedField
<p>Question about PrimaryKeyRelatedField serialization in Django DRF version 3.4.7.</p>

<h1>models</h1>

<pre><code>class UserModel(AbstractEmailUser):

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.email)
class Conversation(models.Model):
    admin = models.ForeignKey('UserModel', db_index=True, related_name='admin_user')
    patient = models.ForeignKey('UserModel', db_index=True)

    subject = models.CharField(max_length=255, db_index=True)

    user_reply = models.BooleanField(default=False)
    admin_seen = models.BooleanField(default=False)

    expire = models.DateTimeField()

    conversation_type = models.ForeignKey('ConversationType', db_index=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.admin)


class ConversationMessages(models.Model):

    text = models.TextField(db_index=True)

    conversation = models.ForeignKey('Conversation', db_index=True, related_name='msg_conv')
    user = models.ForeignKey('UserModel', db_index=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.user)


class ConversationFiles(models.Model):
    message = models.ForeignKey('ConversationMessages', db_index=True, related_name='message')
    file = models.FileField(upload_to='conversations', db_index=True)

    def __str__(self):
        return str(self.user)
</code></pre>

<p>Every model has related field for Rest Framework.
Logic is create conversation, then take ID from conversation and save message model.</p>

<h1>serialize's</h1>

<pre><code>class MessagesSerializer(serializers.ModelSerializer):
    text = serializers.CharField(required=False)
    conversation = serializers.PrimaryKeyRelatedField(queryset=Conversation.objects.all(), required=False)
    user = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all(), required=False)

    class Meta:
        model = ConversationMessages



class ConversationSerializer(serializers.ModelSerializer):

    admin = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all(), required=False)
    msg_conv = MessagesSerializer()

    class Meta:
        model = Conversation

    def create(self, validated_data):
        msg_conv = validated_data.pop('msg_conv', None)
        admin_user = Conversation.objects.create(**validated_data)
        ConversationMessages.objects.create(conversation_id=Conversation.objects.get(id=admin_user.id).id, **msg_conv)

        return admin_user
</code></pre>

<p>Serializer is problem on POST method. Everything works great POST object create data in database, but problem is when serializer save object i get this message: <strong>'RelatedManager' object has no attribute 'conversation'</strong>.</p>

<h1>View</h1>

<pre><code>class ConversationView(APIView):

    authentication_classes = (JSONWebTokenAuthentication,)

    @parser_classes((FormParser, MultiPartParser, FileUploadParser))
    def post(self, request):
        admin = request.user.id
        data = request.data

        my_time = datetime.datetime.strptime('07/05/15', '%m/%d/%y')
        my_time = my_time.replace(hour=23, minute=59)

        data['admin'] = admin
        data['expire'] = my_time

        data['msg_conv']['user'] = admin

        serializer = ConversationSerializer(data=data)

        if serializer.is_valid():
            serializer.save()

            return Response(data={'success': True, 'user': serializer.data}, status=status.HTTP_200_OK)
        else:
            return Response(data={'success': False, 'msg': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
</code></pre>

<h1>POST json</h1>

<pre><code>{

        "subject":"aloha",
        "conversation_type":1,
        "patient":3,

    "msg_conv":{
        "text":"ovo je neki teks"
    }
}
</code></pre>

<p>Can't figure out how to return data from serializer to view.</p>

<p>Django version : 1.10.2
Python: 3.4.3</p>

<p>The issue is there:</p>

<pre><code>class ConversationSerializer(serializers.ModelSerializer):

    msg_conv = MessagesSerializer()
</code></pre>

<p>By doing this, you are saying that <code>Conversation</code> has a FK to <code>Message</code>. Therefore DRF tries to do the mapping and fails because it's the oposit.</p>

<p>You just need to add the <code>many=True</code> argument to let DRF knows this is a reversed FK.</p>

python json django serialization django-rest-framework