0
我想我的序列化對象queryset
並得到如下錯誤,
錯誤而在Django休息序列化QuerySet對象框架
Exception Type: AttributeError
Exception Value:
Got AttributeError when attempting to get a value for field `fullName` on serializer `UserSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'fullName'.
回溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/user/crud/
Django Version: 1.9
Python Version: 2.7.10
Installed Applications:
['rest_framework',
'background_task',
'userApp',
'imageUpload',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\django\core\handlers\base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\django\core\handlers\base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\rest_framework\viewsets.py" in view
87. return self.dispatch(request, *args, **kwargs)
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\rest_framework\views.py" in dispatch
466. response = self.handle_exception(exc)
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\rest_framework\views.py" in dispatch
463. response = handler(request, *args, **kwargs)
File "C:\Users\JpG\Desktop\CYC_Heroku\userApp\views.py" in create
24. return Response(data=serializer.data) # {"detail": "Mobile Number already exist"})
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\rest_framework\serializers.py" in data
503. ret = super(Serializer, self).data
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\rest_framework\serializers.py" in data
239. self._data = self.to_representation(self.instance)
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\rest_framework\serializers.py" in to_representation
463. attribute = field.get_attribute(instance)
File "C:\Users\JpG\Desktop\herokuEnv\lib\site-packages\rest_framework\fields.py" in get_attribute
422. raise type(exc)(msg)
Exception Type: AttributeError at /user/crud/
Exception Value: Got AttributeError when attempting to get a value for field `fullName` on serializer `UserSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'fullName'.
我在內的一些代碼片段在這裏。
views.py
class UserView(viewsets.ModelViewSet):
queryset = UserModel.objects.all()
serializer_class = UserSerializer
def create(self, request, *args, **kwargs):
mobNum = request.data.get('mobileNum')
if UserModel.objects.filter(mobileNum=mobNum).exists():
serializer=UserSerializer(UserModel.objects.filter(mobileNum=mobNum))
return Response(data=serializer.data)
serializer.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = UserModel
fields = '__all__'
models.py
class UserModel(models.Model):
fullName = models.CharField(max_length=100)
mobileNum = models.CharField(max_length=12)
isAuthority = models.BooleanField(default=False)
其實,我期待類似於下面的代碼
class UserView(viewsets.ModelViewSet):
queryset = UserModel.objects.all()
serializer_class = UserSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(data=serializer.data)
你可以把你的usermodel和你發佈的數據 – Cadmus
@SnakeFcz更新問題與models.py和輸入截圖 –