title: Posting a foreign key relationship in Django Rest Framework
body: <p>In my <strong>models</strong>, I have the following classes:</p>
  
  <pre><code>class Topic(models.Model):
      name = models.CharField(max_length=25, unique=True)
  
  class Content(models.Model):
      title = models.CharField(max_length=255)
      body = models.TextField()
      topic = models.ForeignKey(Topic, blank=True, null=True)
  </code></pre>
  
  <p>My <strong>serializers</strong> is like this:</p>
  
  <pre><code>class TopicSerializer(serializers.ModelSerializer):
       class Meta:
           model = Topic
           fields = ('name')
  
  class ContentSerializer(serializers.ModelSerializer):
       topic = TopicSerializer(read_only=True)
       class Meta:
            model = Content
            fields = ('title', 'body', 'topic')
  </code></pre>
  
  <p>Alright, so in my <strong>urls</strong> file, I have the following pattern:</p>
  
  <pre><code>urlpatterns = [
      ...
      url(r'^api/topic_detail/(?P&lt;name&gt;[a-zA-Z0-9-]+)/content_list/$', views.topic_content_list, name='topic_content_list'),
      ...
  ]
  </code></pre>
  
  <p>Therefore, when the user goes to say <code>/api/topic_detail/sports/content_list/</code>, we get a list of all the contents that has the topic of <strong>sports</strong>. Now what I want is if we <strong>POST</strong> the following data to the above URL, then a Content object is created with the topic field related automatically to sports. </p>
  
  <p>I am trying to do this the following way in my <strong>views</strong>:</p>
  
  <pre><code>@api_view(['GET', 'POST'])
  def topic_content_list(request, name):
      try:
          topic = Topic.objects.get(name=name)
      except:
          return Response(status=status.HTTP_404_NOT_FOUND)
  
      if request.method == 'GET':
          contents = Content.objects.filter(topic=topic)
          serializer = ContentSerializer(contents, many=True)
          return Response(serializer.data)
  
      elif request.method == 'POST':
          request.data["topic"] = topic
          serializer = ContentSerializer(data=request.data)
          print request.data
          if serializer.is_valid():
              serializer.save()
              return Response(serializer.data, status=status.HTTP_201_CREATED)
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>Now lets say I go the URL <code>/api/topic_detail/sports/content_list/</code> and POST this:</p>
  
  <pre><code>{
      "title": "My blog post",
      "body" : ".....",
  }
  </code></pre>
  
  <p>The content object gets created and the title and body field is set properly. However, the topic field is set to null. How can I fix this? Any help is appreciated.</p>
  
  <p>Also, please don't suggest using generic viewsets, as I am uncomfortable with so many things happening automatically.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Alright, so I fixed my dumb mistake : </p>
  
  <pre><code>class ContentSerializer(serializers.ModelSerializer):
       topic = TopicSerializer(read_only=False)
       class Meta:
            model = Content
            fields = ('title', 'body', 'topic')
  </code></pre>
  
  <p>That is, I set the read_only argument to False. However, now the post creates a new error:</p>
  
  <pre><code>{
      "topic": {
          "non_field_errors": [
              "Invalid data. Expected a dictionary, but got Topic."
          ]
      }
  }
  </code></pre>
  
  <p>This I am pretty sure refers to the fact that the data.website I'm sending in is not JSON, but instead just a Django model instance. How can I JSONify the single instance?</p>