title: Django rest: AJAX Post is not working, 400 response
body: <p>I am newbie to DRF and Ajax, my serializer code</p>
  
  <pre><code>class CartItem(models.Model):
      cart_id = models.CharField('Cart ID', max_length=50)
      shop =   models.ForeignKey(Shop)
      date_added = models.DateTimeField('Added on', auto_now_add=True)
      quantity = models.IntegerField('Quantity', default=1)
      product = models.ForeignKey(Products,)
      bylist = models.CharField('Item', max_length=150, null=True, blank=True)
      image = models.ImageField('Photo', upload_to='order/%Y',null=True, blank=True)
      image_thumb        =   ImageSpecField(source='image',
                                              processors=[Transpose(),ResizeToFill(200, 200)],
                                              format='JPEG',
                                          options={'quality': 95})
      comment = models.CharField('Comment', max_length=150, null=True, blank=True)
  
  class ShopSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = Shop
          fields = "__all__"
  
  class ProductsSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = Products
          fields = "__all__"          
  
  class CartItemSerializer(serializers.HyperlinkedModelSerializer):
  
      class Meta:
          model = CartItem
          exclude = ('cart_id',)
  </code></pre>
  
  <p>Trying to test the POST call with below json, but getting 400 - Bad request. Tried many different ways but couldn't solve. Please help</p>
  
  <pre><code>function create_post(shop, pd) {    
      jQuery.ajax({
              type: "POST",
              beforeSend: function (request)
              {
                  request.setRequestHeader("X-CSRF-TOKEN", "${_csrf.token}");
              },
              url: "/api/cart/",
              data: JSON.stringify({"shop":"http://127.0.0.1:8000/api/shop/1/","product":"http://127.0.0.1:8000/pd/10/",}),
              //console.log(data);
              success : function(result) {
              console.log("Success!");
     }
  });
  }
  </code></pre>
  
  <p>Also Tried with json:</p>
  
  <pre><code>var json = '{"shop":'+1+',"product":'+2+',"quantity": null,"bylist": "","image": null,"comment": "",}'
  </code></pre>
  
  <p>EDIT: my url conf;</p>
  
  <pre><code>url(r'^api/cart/$', views.CartItemList.as_view(), name='cartitem-list'),
      url(r'^api/cart/(?P&lt;pk&gt;\w+)/$', views.CartItemDetail.as_view(), name='cartitem-detail'),
      url(r'^api/shop/(?P&lt;pk&gt;\w+)/$', views.ShopDetail.as_view(), name='shop-detail'),
      url(r'^api/pd/(?P&lt;pk&gt;\w+)/$', views.ProductsDetail.as_view(), name='products-detail'),
  </code></pre>
  
  <p>view:</p>
  
  <pre><code>class CartItemList(generics.ListCreateAPIView):
      model = CartItem
      queryset = CartItem.objects.all()
      serializer_class = CartItemSerializer
      permission_classes = [
          permissions.AllowAny
      ]
      def perform_create(self, serializer):
          print 'create_view_invoked'
          if self.request.session.get(CART_ID_SESSION_KEY,'') == '':
              self.cart_id = ''
              characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&amp;*()'
              cart_id_length = 50
              for y in range(cart_id_length):
                  self.cart_id += characters[random.randint(0, len(characters)-1)]
              print self.cart_id
              self.request.session[CART_ID_SESSION_KEY] = self.cart_id
          print self.request.session[CART_ID_SESSION_KEY]
          serializer.save(cart_id=self.request.session[CART_ID_SESSION_KEY])
  
      def get_queryset(self):
          print 'get_view_invoked'
          if self.request.session.get(CART_ID_SESSION_KEY,'') == '':
              self.cart_id=''        
          else:
              self.cart_id = self.request.session[CART_ID_SESSION_KEY]
              print self.cart_id
          return CartItem.objects.filter(cart_id=self.cart_id)
  </code></pre>
  
  <p>Forgot to mention, it is working fine directly from DRF API UI (<a href="http://127.0.0.1/api/cart/" rel="nofollow noreferrer">http://127.0.0.1/api/cart/</a>).</p>
  
  <p>I am not seeing any errors in django, only 400 is shown in log.</p>
  
  <pre><code>[03/Apr/2017 19:28:04] "POST /api/cart/ HTTP/1.1" 400 74
  [03/Apr/2017 19:28:07] "POST /api/cart/ HTTP/1.1" 400 74
  [03/Apr/2017 19:28:09] "POST /api/cart/ HTTP/1.1" 400 74
  </code></pre>