2👍
Serializer has request
object in his context dict. So you can get the current user from there.
def get_companies(self, obj):
user = self.context['request'].user
if user.is_staff:
serializer = CompanySerializer(Company.objects.all(), many=True)
else:
serializer = CompanySerializer(instance=companies, many=True)
return serializer.data
0👍
To access request object in serializer you need to pass it context dict while initializing serializer i.e in your views.py
serializer = BUserSerializerRelated(request.user, context={'request': self.request})
then in your serializers.py
, you can access request user object like
user = self.context['request'].user
- [Answered ]-How to specify authentication info to memcache with django?
- [Answered ]-Highcharts using Django-Chartit. Chart lines not displayed without window resize, or Inspect Element
- [Answered ]-How to get jquery ajax error data, and is it the correct way to respond?
- [Answered ]-Git- made changes on a local branch, checked out master, and master is now broken
Source:stackexchange.com