I am creating a call in DRF to POST to the site. I am a bit confused why I get this error. I do not understand why this expects it to be a str. I know there are similiar posts but they still elude be to the solution.
Error
AttributeError: 'str' object has no attribute 'id'
[03/Apr/2018 10:55:12] "POST /api/user/UserProfile/1/ HTTP/1.1" 500 83982
serializer.py looks like and if I remove instance.id i get the same error but just for the next line.
from rest_framework import serializers
from api.models import UserProfile
from django.contrib.auth.models import User
class UserProfileSerializer(serializers.Serializer):
# id = serializers.UUIDField()
class Meta:
model = UserProfile
def create(self, validated_data):
return UserProfile.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.id = validated_data.get('profile_id', str(instance.id))
instance.user_id = validated_data.get('user_id', instance.user_id)
instance.save()
return instance
View
@csrf_exempt
def user_profile_list(request, user_id):
try:
userid = user_id
except userid.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
uprofile = UserProfile.objects.all().values()
list2 = list(uprofile)
return JsonResponse(list2, safe=False)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = UserProfileSerializer(userid, data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
urls.py
url(r'^api/user/UserProfile/(?P<user_id>[0-9]+)/$', userprofile.user_profile_list),
instanceis getting astr.