How to solve TypeError: create() got an unexpected keyword argument in create function of my view in DRF
How to solve TypeError: create() got an unexpected keyword argument in create function of my view in DRF
我遇到的问题是 我无法成功接收与我的模型 ID 相关的 kwarg Station
,因此我无法通过我的视图创建我的记录创建函数。
尽管我可以在 get_queryset
函数中接收 kwarg,但这在 create
函数中是不可能的。
如果我只在我的序列化器中保留创建函数,我可以成功创建记录,但我想添加逻辑和 return 从视图到序列化器创建的记录,而不是直接从创建记录序列化程序。
这是我的片段:
url
path('availability/<uuid:station_id>/', StationAvailabilityList.as_view(),name = 'station_availability_list'),
查看
class StationAvailabilityList(generics.ListCreateAPIView):
serializer_class = StationAvailabilitySerializer
permission_classes = [IsChargingStationOwnerWithStation | IsReadOnlyStations]
def get_queryset(self):
station_owner = self.request.user
station_id = self.kwargs['station_id']
station = Station.objects.get(id = station_id)
availabilities = StationAvailability.objects.filter(station = station)
return availabilities
def create(self, request):
station_id = self.kwargs['station_id']
availability_data = request.data
return super().create(request)
get_queryset()
工作正常,我可以获得 url 中给定的 Station 记录的 ID,但在创建时出现错误:
TypeError: create() got an unexpected keyword argument 'station_id'
这是完整的回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/api/stations/availability/db2059a0-008a-4689-88ca-a1c5bde5a1af/
Traceback (most recent call last):
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/generics.py", line 242, in post
return self.create(request, *args, **kwargs)
Exception Type: TypeError at /api/stations/availability/db2059a0-008a-4689-88ca-a1c5bde5a1af/
Exception Value: create() got an unexpected keyword argument 'station_id'
序列化器/创建函数:
def create(self, validated_data):
print(validated_data)
station_data = validated_data.pop('station')
validated_data_station_id = station_data.pop('id')
station_id = uuid.UUID(validated_data_station_id)
station = Station.objects.get(id=station_id)
station_availability = StationAvailability.objects.create(station=station,**validated_data)
return station_availability
谁能帮我解决这个问题?
def create(self, request, **kwargs):
station_id = kwargs['station_id']
availability_data = request.data
return super().create(request)
我遇到的问题是 我无法成功接收与我的模型 ID 相关的 kwarg Station
,因此我无法通过我的视图创建我的记录创建函数。
尽管我可以在 get_queryset
函数中接收 kwarg,但这在 create
函数中是不可能的。
如果我只在我的序列化器中保留创建函数,我可以成功创建记录,但我想添加逻辑和 return 从视图到序列化器创建的记录,而不是直接从创建记录序列化程序。
这是我的片段:
url
path('availability/<uuid:station_id>/', StationAvailabilityList.as_view(),name = 'station_availability_list'),
查看
class StationAvailabilityList(generics.ListCreateAPIView):
serializer_class = StationAvailabilitySerializer
permission_classes = [IsChargingStationOwnerWithStation | IsReadOnlyStations]
def get_queryset(self):
station_owner = self.request.user
station_id = self.kwargs['station_id']
station = Station.objects.get(id = station_id)
availabilities = StationAvailability.objects.filter(station = station)
return availabilities
def create(self, request):
station_id = self.kwargs['station_id']
availability_data = request.data
return super().create(request)
get_queryset()
工作正常,我可以获得 url 中给定的 Station 记录的 ID,但在创建时出现错误:
TypeError: create() got an unexpected keyword argument 'station_id'
这是完整的回溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/api/stations/availability/db2059a0-008a-4689-88ca-a1c5bde5a1af/
Traceback (most recent call last):
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/home/giorgos/.local/share/virtualenvs/ev-loader-backend-tcqKXHeF/lib/python3.7/site-packages/rest_framework/generics.py", line 242, in post
return self.create(request, *args, **kwargs)
Exception Type: TypeError at /api/stations/availability/db2059a0-008a-4689-88ca-a1c5bde5a1af/
Exception Value: create() got an unexpected keyword argument 'station_id'
序列化器/创建函数:
def create(self, validated_data):
print(validated_data)
station_data = validated_data.pop('station')
validated_data_station_id = station_data.pop('id')
station_id = uuid.UUID(validated_data_station_id)
station = Station.objects.get(id=station_id)
station_availability = StationAvailability.objects.create(station=station,**validated_data)
return station_availability
谁能帮我解决这个问题?
def create(self, request, **kwargs):
station_id = kwargs['station_id']
availability_data = request.data
return super().create(request)