I am faced with the problem that I can not successfully receive the kwarg's related with the id of my model Station
and as a result I can not create my record through my view in the create function.
Despite the fact that I can receive the kwarg's in the get_queryset
function , this is not possible in create
function.
If I keep the create function only in my serializer I can create the record successfully, but instead I want to add logic and return the created record from the view to serializer and not directly create the record from serializer.
Here is my snippets:
url
path('availability/<uuid:station_id>/', StationAvailabilityList.as_view(),name = 'station_availability_list'),
view
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)
The get_queryset()
works fine, i can get the id of the Station record given in the url, but in the create I get the error :
TypeError: create() got an unexpected keyword argument 'station_id'
Here is the full traceback:
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'
serializer/ create function:
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
Can anyone help me solve this issue?
def create(self, request, **kwargs):
station_id = kwargs['station_id']
availability_data = request.data
return super().create(request)