One way is answer from 
this 
https://stackoverflow.com/questions/24164160/adding-extra-data-to-django-rest-framework-results-for-entire-result-set

If it was a simple APIView - You can pass extra context dictionary to your 
serializer and then you can override the __init__ and/or to_representation 
of your serializer and access the passed context there.

for exa.
in your view:
serializer = MySerializer(queryset, context = {'user':request.user })


Your serializer:
class MySerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
user = kwargs.get('context').get('user', None)
if user:
self.user = user
super(MySerializer, self).__init__(*args, **kwargs)


def to_representation(self, instance):
data = super().to_representation(instance)

if(hasattr(self, "user")):
data['user'] = self.user

return data

On Thursday, 4 March 2021 at 18:40:32 UTC+5:30 mayu...@gmail.com wrote:

> please can anyone tell me without creating field how i can add key 
> value in list api view in using model mixin  
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/56530683-27f3-43c2-8f29-34cebc28bdd1n%40googlegroups.com.

Reply via email to