Issue
Hello i am trying to develop a simple REST API endpoint using Django rest framework.I tried checking similar questions but did not work.I want my output as (JSON Format):
{
{
"id": 1,
"status": "ONLINE"
},
{
"id": 2,
"status": "OFFLINE"
}
}
but my output is (List Format):
[
{
"id": 1,
"status": "ONLINE"
},
{
"id": 2,
"status": "OFFLINE"
}
]
My models.py:
class Device(models.Model):
status = models.CharField(max_length=10, default="OFFLINE")
my serializer.py:
class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = Device
fields = '__all__'
and my views.py:
def device_list(request):
devices = Device.objects.all()
serializer = DeviceSerializer(devices, many=True)
return Response(serializer.data)
Solution
def device_list(request):
devices = Device.objects.all()
serializer = DeviceSerializer(devices, many=True)
return Response({"data":serializer.data})
Answered By - Surya Pratap Rana
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.