Discussion:
adding suucees or error message in json response
o***@gmail.com
2018-07-30 11:51:58 UTC
Permalink
Hi All my project on django rest framework

i am getting json response as raw model class

[
{
"id": 1,
"client_name": "google",
"client_logo": "oo"
}
]

but i want to add success message in json response like this?

{
"success":"data fetch successfully"
[
{
"id": 1,
"client_name": "google",
"client_logo": "oo"
}
]
}

how can i do this??

here is all the details

my model is like this

class Client1(models.Model):
client_name = models.CharField(max_length=100, unique =True)
client_logo = models.CharField(max_length=300, null=True)



seraliazer class is


class ClientSerailizers(serializers.ModelSerializer):
class Meta:
model = Client1
fields = ('id', 'client_name', 'client_logo')


and view is


class SnippetList(APIView):
def get(self, request, *args, **kwargs):

client = Client1.objects.all()
success = {"response": "success"}

serializer = ClientSerailizers(client, many=True)


return Response(serializer.data, status=status.HTTP_201_CREATED)



s
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-07-30 13:43:48 UTC
Permalink
You should rely on http success/error codes for your responses. Rely on
those rather than custom messages
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Musharaf Baig
2018-08-01 21:11:15 UTC
Permalink
You can override the .to_representation for that specific purpose. I would
recommend to use status codes.
Post by Jason
You should rely on http success/error codes for your responses. Rely on
those rather than custom messages
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Wanderley S
2018-08-02 02:16:02 UTC
Permalink
Hi!

I believe you might pass a "message" inside the "Response" like so:

return Response(serializer.data, status=status.HTTP_201_CREATED,
message="success")

No quite sure if this is wrong though.
Post by o***@gmail.com
Hi All my project on django rest framework
i am getting json response as raw model class
[
{
"id": 1,
"client_name": "google",
"client_logo": "oo"
}
]
but i want to add success message in json response like this?
{
"success":"data fetch successfully"
[
{
"id": 1,
"client_name": "google",
"client_logo": "oo"
}
]
}
how can i do this??
here is all the details
my model is like this
client_name = models.CharField(max_length=100, unique =True)
client_logo = models.CharField(max_length=300, null=True)
seraliazer class is
model = Client1
fields = ('id', 'client_name', 'client_logo')
and view is
client = Client1.objects.all()
success = {"response": "success"}
serializer = ClientSerailizers(client, many=True)
return Response(serializer.data, status=status.HTTP_201_CREATED)
s
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...