【发布时间】:2021-04-28 02:03:18
【问题描述】:
我在向 UserRegisterView 发送请求时收到以下错误:
File "/Users/MichaelAjanaku/Desktop/test/afrocode/lib/python3.6/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/MichaelAjanaku/Desktop/test/leave/views.py", line 22, in post
serializer.save()
File "/Users/MichaelAjanaku/Desktop/test/afrocode/lib/python3.6/site-packages/rest_framework/serializers.py", line 207, in save
'`create()` did not return an object instance.'
AssertionError: `create()` did not return an object instance.
我不知道为什么会这样。这是我的views.py:
class UserRegistrationView(CreateAPIView):
serializer_class = UserRegistrationSerializer
permission_classes = (AllowAny,)
def post(self, request):
serializer = self.serializer_class(data= request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
status_code = status.HTTP_201_CREATED
response = {
'success' : 'True',
'status code' : status_code,
'message' : 'User registered successfully'
}
和序列化程序:
class UserRegistrationSerializer(serializers.ModelSerializer):
profile = UserSerializer(required=False)
class Meta:
model = User
fields = ('email', 'password','profile' )
extra_kwargs = {'password': {'write_only' : True}}
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create_user(**validated_data)
UserProfile.objects.create(
user = user,
first_name = profile_data['first_name'],
last_name= profile_data['last_name'],
)
请问是什么原因导致的错误?谢谢
【问题讨论】:
标签: python django django-rest-framework