Reputation: 2123
The create function is giving me the following error:
AttributeError: 'dict' object has no attribute 'user'
Looks like request.user does not exist in this serializer. Is there a way to retrieve the user id in serializer? Or is this not possible?
class EmployeeQuestionSerializer(serializers.ModelSerializer):
class Meta:
model = EmployeeQuestion
fields = (
'id',
'employee',
'question',
'attempted',
'passed',
)
def create(self, request):
serializer = EmployeeQuestionSerializer(employee=request.user.pk)
if serializer.is_valid():
return serializer.save();
def update(self, instance, validated_data):
instance.attempted = validated_data.get('attempted')
instance.passed = validated_data.get('passed')
instance.save()
return instance
Upvotes: 2
Views: 875
Reputation: 332
You can to pass the request object from views.py to this serializer
serializer = EmployeeQuestionSerializer(queryset, context={'request':request})
then in serializers.py you can access that with self inside create:
user=self.context['request'].user
Upvotes: 5