title: Getting "Bad Request" on the post request to Django Rest-Api using serializers
body: <p>I am trying to save data into a model using django-rest framework. I have already written the api, it works fine when i access it directly using the url to api. But I get a bad-request error when i try to post data using ajax.
  If it is working fine when data is inserted using the api interface, it should work fine when data is inserted using ajax....but instead i am getting a bad request. </p>
  
  <p>here is the AJAX request method(Jquery):</p>
  
  <pre><code>$("form").submit(function(event){
             event.preventDefault();
  
              var this_ = $(this);
              var formData =this_.serialize();
              $.ajax({
                  url: "/api/forum/posts/{{ post_question.thread_id.id }}/create",
                  data: formData,
                  method: "POST",
                  success: function (data) {
                      console.log("successfully returned");
                      console.log(data);
                      displayPosts();
  
                  },
           })
  </code></pre>
  
  <p>Serializers are as follow, in api/serializers.py : </p>
  
  <pre><code>class ThreadModelSerializer(serializers.ModelSerializer):
      created_by = UserDisplaySerializer(read_only=True)
      class Meta:
         model = Thread
         fields = '__all__'
  
  
  class PostModelSerializer(serializers.ModelSerializer):
      posted_by = UserDisplaySerializer(read_only=True)
      class Meta:
         model = Post
         fields = '__all__'
  </code></pre>
  
  <p>Models for the Post and Thread are as follow in models.py:</p>
  
  <pre><code>class Thread(models.Model):
      thread_subject = models.CharField(max_length=250)
      posted_on = models.DateTimeField(auto_now=True)
      category = models.CharField(max_length=250)
      forum_id = models.ForeignKey(Forum, on_delete=models.CASCADE)
      created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  
  
  class Post(models.Model):
      post_message = models.TextField()
      thread_id = models.ForeignKey(Thread, on_delete=models.CASCADE)
      parent_id = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)
      posted_on = models.DateTimeField(auto_now=True)
      posted_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  
  </code></pre>
  
  <p>and finally the API view is like this, in api/views.py:</p>
  
  <pre><code>class PostCreateApiView(generics.CreateAPIView):
      serializer_class = PostModelSerializer
      permission_classes = [permissions.IsAuthenticated]
  
      def perform_create(self, serializer, *args, **kwargs):
          thread_id = self.kwargs.get('thread_id', None)
          thread = get_object_or_404(Thread, id=thread_id)
          parent_post = get_object_or_404(Post, thread_id=thread_id, parent_id__isnull=True)
  
          serializer.save(posted_by=self.request.user, parent_id=parent_post, thread_id=thread)
  </code></pre>
  
  <p>and url route is as follow in api/urls.py :</p>
  
  <pre><code>    path('posts/&lt;int:thread_id&gt;/create', PostCreateApiView.as_view(), name='post_create'),
  </code></pre>
  
  <p>Hopefully I am able to make the problem statement clear, need help.
  When I create a new post using the api url directly, example :</p>
  
  <p><a href="http://localhost:8000/api/forum/posts/4/create" rel="nofollow noreferrer">http://localhost:8000/api/forum/posts/4/create</a></p>
  
  <p>I get no errors and post is successfully inserted in the database.</p>
  
  <p>But when I try to insert the data using the template(ajax), example : </p>
  
  <p><a href="http://localhost:8000/forums/thread/4/" rel="nofollow noreferrer">http://localhost:8000/forums/thread/4/</a></p>
  
  <p>I get a bad request like following : </p>
  
  <pre><code>Bad Request: /api/forum/posts/4/create
  HTTP POST /api/forum/posts/4/create 400 [0.02, 127.0.0.1:40630]
  </code></pre>
  
  <p>Need help, I have tried hard, but can't find the solution. 
  Awaiting reply. Thanks.</p>