我正在尝试实现基本序列化器,并且我遵循“http://www.django-rest-framework.org/api-guide/serializers/#baseserializer”。需要帮助在Django中实现基本序列化器
我的urls.py:
url(r'^auth/myuser/(?P<pk>[0-9]+)/profile/$', UserProfileViewSet.as_view({'patch':'update'}), name='user-profile'),
Views.py:
class UserProfileViewSet(viewsets.ModelViewSet):
queryset = CustomUser.objects.all()
serializer_class = UserProfileSerializer
def get(self,request,pk,*args,**kwargs):
user_instance = CustomUser.objects.get(pk=pk)
dashboard_data = UserProfileSerializer(user_instance)
content = {'result': dashboard_data}
return Response(content)
Serializers.py:
class UserProfileSerializer(serializers.BaseSerializer):
def to_representation(self,obj):
return{
'email':obj.email,
'first_name':obj.first_name,
'last_name':obj.last_name,
'date_of_birth':obj.date_of_birth,
'gender':obj.get_gender_display(),
'location':obj.location,
'calling_code':obj.callingcode,
'phone_primary':obj.phone_primary,
'phone_secondary':obj.phone_secondary,
'website':obj.website
}
但我得到错误“用户对象不是JSON序列化”,并且我没有找到任何不可序列化的用户对象的属性。
我已经在SO上找到了一些答案,但是在django rest框架API指南中找不到类似的步骤。因此寻找与api指南同步的解决方案。
我同意,我相信这个工程。但我想尝试使用Base序列化器来学习。 –