Q:

Override JSON data in Django RestFramwork

Right now with the current Views functions I am getting the data given below:
{"item": "zxmnb",
"category": "zxc",
"price": "zxc",
"restaurant": 1}

Here is my views file:
class RestaurantMenuView(generics.RetrieveAPIView):
    lookup_field = 'item'
    serializer_class = MenuSerializer

    def get_queryset(self):
        return Menu.objects.all()

But the issue is I want the data to be in a format as:
{"restaurant": "name"
"item":"some item",
"category": "some category",
"price": "some price"
}

I want to mention that Restaurant is another model in my models class,Now I know that if I use restaurant I will only get the pk. But what I want is the JSON to be displayed like that. 

A:

Why don't you redefine the to_representation() function. Something like this:
class MenuSerializer(serializers.ModelSerializer):
    def to_representation(self, obj):
        restaurants = RestaurantSerializer(instance=obj, 
        context=self.context).data
        data = []
        for restaurant in restaurants:
            data.append(
                {
                    "restaurant": {
                        "item": obj.item,
                        "category": obj.category,
                        "price": obj.price,
                        "name": restaurant.name,
                    }
                }
            )
        return data

Without looking at your models or why you want restaurant in there, I added the for loop in order to show you that you can pretty much access any of the data in your to_representation() and put it in any sort of format you want. I use this when I'm trying to render my JSON objects into XML in a specific way. Hope this helps. 
Also check out the documentation: 
https://www.django-rest-framework.org/api-guide/serializers/#overriding-serialization-and-deserialization-behavior
Another solution that you could consider is adding a Foreign Key on your Menu model back to the Restaurant, then you could define your serializer like this:
class RestaurantSerializer(serializers.ModelSerializer):
    menu = MenuViewSerializer(read_only=True, many=True)

    class Meta:
        model = Restaurant
        fields = [
            "id",
            "name",
            "menu",
        ]
        extra_kwargs = {
            "menu": {"read_only": True},
        }

