PUT Request fails on field rename
<p>Serializer.py</p>

<pre><code>class CategorySerializer(serializers.ModelSerializer) :
    id = serializers.IntegerField(source='category_id')
    name = serializers.CharField(source='category_name')

    class Meta:
        model = Category
        fields = ['id', 'name']
</code></pre>

<p>Above works fine for the GET but when i run PUT request it goes to fail block </p>

<p>views.py for PUT </p>

<pre><code>request.method == 'PUT':
        serializer = CategorySerializer(category, data=request.data)
        if serializer.is_valid():
            serializer.save()
            response = {
                'status': status.HTTP_200_OK,
                'message' : "Category Updated",
            }
            return HttpResponse(json.dumps(response), content_type='application/json')
        else :
            response = {
                'status': status.HTTP_400_BAD_REQUEST,
                'message' : "Category not found",
            }
            return HttpResponse(json.dumps(response), content_type='application/json')
</code></pre>

<p>And i'm running following curl</p>

<p>curl -X PUT <a href="http://localhost:8000/api/add-category/4/" rel="nofollow">http://localhost:8000/api/add-category/4/</a> -d "category_name=xyz"</p>

<p>Response:</p>

<pre><code>{"status": 400, "message": "Category not found"}
</code></pre>

<p>Each time it goes in else part. </p>

<p>Experts please help</p>

<p>You didn't attach your's serializer errors, but it looks like that you should set <code>partial</code> argument for <code>PUT</code> request method.
Try </p>

<pre><code>serializer = CategorySerializer(category, data=request.data, partial=True)
</code></pre>

<p>Documentation <a href="http://www.django-rest-framework.org/api-guide/serializers/#partial-updates" rel="nofollow">link</a></p>

python django django-rest-framework