Re: Custom-DRF-Response

2020-07-04 Thread Karthik Marudhanayagam
Hi, please refer
https://stackoverflow.com/questions/47173483/django-rest-framework-custom-format-for-all-out-responses

hope this helps.

Best
Karthik

On Sat, Jul 4, 2020 at 5:30 PM Ronaldo Mata  wrote:

> Thx for your answer. 
>
> but this brings me to the second point. If I do that I must to rewrite all
> method in my API Class Based Views to response with this format. I think
> that is not the best way to deal with this problem. What do you think?
>
> El sáb., 4 jul. 2020 a las 7:53, Arpana Mehta ()
> escribió:
>
>> You can use a function which
>>
>> returns Response({'status': 'SUCCESS'})
>>
>> On Sat, 4 Jul 2020, 03:24 Ronaldo Mata,  wrote:
>>
>>> Hi Guys
>>>
>>> Somebody can help me?
>>>
>>> I want to create a custom response with the follow format:
>>>
>>> {
>>>   "status": true,
>>>   "message": "Any message",
>>>   "data": []}
>>>
>>>
>>> but I don't want create a custom_response function because I must to
>>> modified all Api class based views response. What is the best way to deal
>>> with this?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAP%3DoziQW9yHM74Pyc2bYzik0RJFQua1-qWNuE%2B%2B4OU%2BGoutRZA%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAGyqUuVGdobfwZgwmUsWj94YWimTNwhE0aYmi9seZAYU7TkaCw%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAP%3DoziTUi-B3OPZ9U6US8p2Jm%3DXGmrGs%2BHvXBXQvCYDAYjtMTw%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAA0PHa8t6dFWmtYRcd5fsrmWCZ6e14P9PjcEyvVDEz%2BJHejFSA%40mail.gmail.com.


Re: Custom-DRF-Response

2020-07-04 Thread Ronaldo Mata
I Think that this should be a common approach, since many famous frontend
framework is easier to create logic to deal with that format response.

El sáb., 4 jul. 2020 a las 10:00, Ronaldo Mata ()
escribió:

> I think that this not depend of my serializer. I only want that the
> response data will be inside data variable in the JSON response and i need
> to add status variable and message to the JSON response. I can to rewrite
> all Class Based Views of DRF to response with my desired format. but this
> sound not very good.
>
> I can do this:
>
> def post(self, request, *args, **kwargs):
> ...blah...blah
> ...return Response({ 'status': true, 'message': 'login success',
> 'data': {'token':'345rjgjfegjgdsaj,fdgjsafgj'}})
>
> right?
>
> but this required rewrite all DRF methods on Class Based Views to change
> the response format.
>
> I'm trying to use Renderers. I think that is the best for this. But I'm
> not sure how to pass message from views to renderers. it returns me to the
> same problem.
>
> But this required pass from every view message attribute to data (since
> all messages depend on the view) example:
>
> { status: True, message:"user regitered", data: {user: {user detail,
> token...}}} (Register View) --> has custom message
> { status: True, message:"logout successfully", data: {} (Logout View) -->
> has custom message
>
>
> from rest_framework import status
> from rest_framework.renderers import JSONRenderer
>
>
> class GlobalJSONRenderer(JSONRenderer):
>
> def render(self, data, accepted_media_type=None, renderer_context=None
> ):
> """
> this function change the response format to return the follow
> format:
>
> {
> 'status': True o False, -> Depend of status code
> 'message': '', -> A message (not yet)
> 'data': {} -> All Data
> }
> """
> data = {
> 'status': False,
> 'message': '',
> 'data': data
> }
> response = renderer_context['response']
> status_code = response.status_code
>
> if status.is_success(status_code):
> data['status'] = True
>
> return super(GlobalJSONRenderer, self).render(
> data,
> accepted_media_type,
> renderer_context
> )
>
>
> El sáb., 4 jul. 2020 a las 9:34, Julio Cojom ()
> escribió:
>
>> How is your model and your serializer?
>>
>> El sáb., 4 jul. 2020 a las 6:00, Ronaldo Mata ()
>> escribió:
>>
>>> Thx for your answer. 
>>>
>>> but this brings me to the second point. If I do that I must to rewrite
>>> all method in my API Class Based Views to response with this format. I
>>> think that is not the best way to deal with this problem. What do you think?
>>>
>>> El sáb., 4 jul. 2020 a las 7:53, Arpana Mehta ()
>>> escribió:
>>>
 You can use a function which

 returns Response({'status': 'SUCCESS'})

 On Sat, 4 Jul 2020, 03:24 Ronaldo Mata, 
 wrote:

> Hi Guys
>
> Somebody can help me?
>
> I want to create a custom response with the follow format:
>
> {
>   "status": true,
>   "message": "Any message",
>   "data": []}
>
>
> but I don't want create a custom_response function because I must to
> modified all Api class based views response. What is the best way to deal
> with this?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAP%3DoziQW9yHM74Pyc2bYzik0RJFQua1-qWNuE%2B%2B4OU%2BGoutRZA%40mail.gmail.com
> 
> .
>
 --
 You received this message because you are subscribed to the Google
 Groups "Django users" group.
 To unsubscribe from this group and stop receiving emails from it, send
 an email to django-users+unsubscr...@googlegroups.com.
 To view this discussion on the web visit
 https://groups.google.com/d/msgid/django-users/CAGyqUuVGdobfwZgwmUsWj94YWimTNwhE0aYmi9seZAYU7TkaCw%40mail.gmail.com
 
 .

>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> 

Re: Custom-DRF-Response

2020-07-04 Thread Ronaldo Mata
I think that this not depend of my serializer. I only want that the
response data will be inside data variable in the JSON response and i need
to add status variable and message to the JSON response. I can to rewrite
all Class Based Views of DRF to response with my desired format. but this
sound not very good.

I can do this:

def post(self, request, *args, **kwargs):
...blah...blah
...return Response({ 'status': true, 'message': 'login success',
'data': {'token':'345rjgjfegjgdsaj,fdgjsafgj'}})

right?

but this required rewrite all DRF methods on Class Based Views to change
the response format.

I'm trying to use Renderers. I think that is the best for this. But I'm not
sure how to pass message from views to renderers. it returns me to the same
problem.

But this required pass from every view message attribute to data (since all
messages depend on the view) example:

{ status: True, message:"user regitered", data: {user: {user detail,
token...}}} (Register View) --> has custom message
{ status: True, message:"logout successfully", data: {} (Logout View) -->
has custom message


from rest_framework import status
from rest_framework.renderers import JSONRenderer


class GlobalJSONRenderer(JSONRenderer):

def render(self, data, accepted_media_type=None, renderer_context=None):
"""
this function change the response format to return the follow
format:

{
'status': True o False, -> Depend of status code
'message': '', -> A message (not yet)
'data': {} -> All Data
}
"""
data = {
'status': False,
'message': '',
'data': data
}
response = renderer_context['response']
status_code = response.status_code

if status.is_success(status_code):
data['status'] = True

return super(GlobalJSONRenderer, self).render(
data,
accepted_media_type,
renderer_context
)


El sáb., 4 jul. 2020 a las 9:34, Julio Cojom ()
escribió:

> How is your model and your serializer?
>
> El sáb., 4 jul. 2020 a las 6:00, Ronaldo Mata ()
> escribió:
>
>> Thx for your answer. 
>>
>> but this brings me to the second point. If I do that I must to rewrite
>> all method in my API Class Based Views to response with this format. I
>> think that is not the best way to deal with this problem. What do you think?
>>
>> El sáb., 4 jul. 2020 a las 7:53, Arpana Mehta ()
>> escribió:
>>
>>> You can use a function which
>>>
>>> returns Response({'status': 'SUCCESS'})
>>>
>>> On Sat, 4 Jul 2020, 03:24 Ronaldo Mata,  wrote:
>>>
 Hi Guys

 Somebody can help me?

 I want to create a custom response with the follow format:

 {
   "status": true,
   "message": "Any message",
   "data": []}


 but I don't want create a custom_response function because I must to
 modified all Api class based views response. What is the best way to deal
 with this?

 --
 You received this message because you are subscribed to the Google
 Groups "Django users" group.
 To unsubscribe from this group and stop receiving emails from it, send
 an email to django-users+unsubscr...@googlegroups.com.
 To view this discussion on the web visit
 https://groups.google.com/d/msgid/django-users/CAP%3DoziQW9yHM74Pyc2bYzik0RJFQua1-qWNuE%2B%2B4OU%2BGoutRZA%40mail.gmail.com
 
 .

>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAGyqUuVGdobfwZgwmUsWj94YWimTNwhE0aYmi9seZAYU7TkaCw%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAP%3DoziTUi-B3OPZ9U6US8p2Jm%3DXGmrGs%2BHvXBXQvCYDAYjtMTw%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, 

Re: Custom-DRF-Response

2020-07-04 Thread Julio Cojom
How is your model and your serializer?

El sáb., 4 jul. 2020 a las 6:00, Ronaldo Mata ()
escribió:

> Thx for your answer. 
>
> but this brings me to the second point. If I do that I must to rewrite all
> method in my API Class Based Views to response with this format. I think
> that is not the best way to deal with this problem. What do you think?
>
> El sáb., 4 jul. 2020 a las 7:53, Arpana Mehta ()
> escribió:
>
>> You can use a function which
>>
>> returns Response({'status': 'SUCCESS'})
>>
>> On Sat, 4 Jul 2020, 03:24 Ronaldo Mata,  wrote:
>>
>>> Hi Guys
>>>
>>> Somebody can help me?
>>>
>>> I want to create a custom response with the follow format:
>>>
>>> {
>>>   "status": true,
>>>   "message": "Any message",
>>>   "data": []}
>>>
>>>
>>> but I don't want create a custom_response function because I must to
>>> modified all Api class based views response. What is the best way to deal
>>> with this?
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAP%3DoziQW9yHM74Pyc2bYzik0RJFQua1-qWNuE%2B%2B4OU%2BGoutRZA%40mail.gmail.com
>>> 
>>> .
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAGyqUuVGdobfwZgwmUsWj94YWimTNwhE0aYmi9seZAYU7TkaCw%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAP%3DoziTUi-B3OPZ9U6US8p2Jm%3DXGmrGs%2BHvXBXQvCYDAYjtMTw%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHRQUHm1hgchtYWMEHj%2B7PHomErn7TGXD3Vc9SpOwp%2BvGsVx5w%40mail.gmail.com.


Re: Custom-DRF-Response

2020-07-04 Thread Ronaldo Mata
Thx for your answer. 

but this brings me to the second point. If I do that I must to rewrite all
method in my API Class Based Views to response with this format. I think
that is not the best way to deal with this problem. What do you think?

El sáb., 4 jul. 2020 a las 7:53, Arpana Mehta ()
escribió:

> You can use a function which
>
> returns Response({'status': 'SUCCESS'})
>
> On Sat, 4 Jul 2020, 03:24 Ronaldo Mata,  wrote:
>
>> Hi Guys
>>
>> Somebody can help me?
>>
>> I want to create a custom response with the follow format:
>>
>> {
>>   "status": true,
>>   "message": "Any message",
>>   "data": []}
>>
>>
>> but I don't want create a custom_response function because I must to
>> modified all Api class based views response. What is the best way to deal
>> with this?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAP%3DoziQW9yHM74Pyc2bYzik0RJFQua1-qWNuE%2B%2B4OU%2BGoutRZA%40mail.gmail.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGyqUuVGdobfwZgwmUsWj94YWimTNwhE0aYmi9seZAYU7TkaCw%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAP%3DoziTUi-B3OPZ9U6US8p2Jm%3DXGmrGs%2BHvXBXQvCYDAYjtMTw%40mail.gmail.com.


Re: Custom-DRF-Response

2020-07-04 Thread Arpana Mehta
You can use a function which

returns Response({'status': 'SUCCESS'})

On Sat, 4 Jul 2020, 03:24 Ronaldo Mata,  wrote:

> Hi Guys
>
> Somebody can help me?
>
> I want to create a custom response with the follow format:
>
> {
>   "status": true,
>   "message": "Any message",
>   "data": []}
>
>
> but I don't want create a custom_response function because I must to
> modified all Api class based views response. What is the best way to deal
> with this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAP%3DoziQW9yHM74Pyc2bYzik0RJFQua1-qWNuE%2B%2B4OU%2BGoutRZA%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAGyqUuVGdobfwZgwmUsWj94YWimTNwhE0aYmi9seZAYU7TkaCw%40mail.gmail.com.


Custom-DRF-Response

2020-07-03 Thread Ronaldo Mata
Hi Guys

Somebody can help me?

I want to create a custom response with the follow format:

{
  "status": true,
  "message": "Any message",
  "data": []}


but I don't want create a custom_response function because I must to
modified all Api class based views response. What is the best way to deal
with this?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAP%3DoziQW9yHM74Pyc2bYzik0RJFQua1-qWNuE%2B%2B4OU%2BGoutRZA%40mail.gmail.com.