我面临的问题是我无法成功接收与我的模型Station
的id相关的kwarg,因此我无法通过创建函数中的视图创建我的记录
尽管我可以在get_queryset
函数中接收kwarg,但这在create
函数中是不可能的。
如果我只在序列化程序中保留create函数,我可以成功创建记录,但我希望添加逻辑并将创建的记录从视图返回到序列化程序,而不是直接从序列化程序创建记录。
以下是我的片段:
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)