Rename response fields django rest framework serializer
<p>I'm calling a simple get API using djangorestframework. My Model is </p>

<pre><code>class Category(models.Model):
    category_id = models.AutoField(primary_key=True)
    category_name = models.CharField("Category Name", max_length = 30)
    category_created_date = models.DateField(auto_now = True, auto_now_add=False)
    category_updated_date = models.DateField(auto_now = True, auto_now_add=False)

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

<p>serializer.py</p>

<pre><code>class CategorySerializer(serializers.ModelSerializer) :
    class Meta:
        model = Category
        fields = ['category_id', 'category_name']

def category_list(request):
    if request.method == 'GET':
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        return Response(serializer.data)
</code></pre>

<p>It's working fine when i hit request on the URL and returning following response.</p>

<pre><code>[
    {
        "category_id": 1,
        "category_name": "ABC"
    }
]
</code></pre>

<p>i want to change the response field names as it's for my DB only and don't want to disclose in response. If i change the name in serializer class than it's giving no field match error.</p>

<p>Also i want to customise other params like above response in response object with message and status like below.</p>

<pre><code>{
status : 200,
message : "Category List",
response : [
        {
            "id": 1,
            "name": "ABC"
        }
    ]
}
</code></pre>

<p>Need a proper guide and flow. Experts help.</p>

<p><strong>First</strong> of all using <code>category_</code> in field names is redundant. Because you are already assigning this fields to <code>Category</code> model, and by doing this you are creating "namespace" for this fields.</p>

<pre><code>class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField("Category Name", max_length = 30)
    created_date = models.DateField(auto_now = True, auto_now_add=False)
    updated_date = models.DateField(auto_now = True, auto_now_add=False)

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

<p><strong>Second</strong> In django <code>id</code> AutoField is created automatically why would you need set it explicitly?</p>

<p><strong>And answering your question</strong> There is <a href="http://www.django-rest-framework.org/api-guide/fields/#source" rel="nofollow"><code>source</code></a> parameter in serializer fields.</p>

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

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

<p>And than you can change your response manually</p>

<pre><code>from rest_framework import status

def category_list(request):
    if request.method == 'GET':
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True)
        response = {
            'status': status.HTTP_200_OK,
            'message' : "Category List",
            'response' : serializer.data
        }
        return Response(response)
</code></pre>

python django django-rest-framework jsonserializer