I'm going to use UpdateAPIView in django. When I completed the view and ran the server, the following error occurred.
Expected view updateViewSet to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.
I wonder why I shouldn't designate lookup_field as pk in my code and what I can use instead of pk. Here is my code
serializers.py
from rest_framework import serializers
from .models import arduino
class arduinoSerializers (serializers.ModelSerializer) :
name = serializers.CharField(source='name.username', read_only=True)
class Meta :
model = arduino
fields = ['name', 'temp', 'humi']
views.py
from .models import arduino
from .serializers import arduinoSerializers
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework.generics import CreateAPIView, UpdateAPIView
class arduinoViewSet (ViewSet) :
serializer_class = arduinoSerializers
def data (self, request) :
queryset = arduino.objects.filter(name=self.request.user)
serializer = arduinoSerializers(queryset, many=True)
return Response(serializer.data)
class createViewSet (CreateAPIView) :
serializer_class = arduinoSerializers
def perform_create (self, serializer) :
serializer.save(name=self.request.user)
def get_queryset (self) :
user = self.request.user
return arduino.objects.filter(name=user)
def dataReceive (self, request) :
queryset = self.get_queryset()
serializer = arduinoSerializers(queryset, many=True)
if serializer.is_valid() :
perform_create(serializer)
return Response(serializer.data)
class updateViewSet (UpdateAPIView) :
serializer_class = arduinoSerializers
lookup_field = 'pk'
def perform_update (self, serializer) :
serializer.update(name=self.request.user)
def get_queryset (self) :
user = self.request.user
return arduino.objects.filter(name=user)
def dataReceive (self, request) :
queryset = self.get_queryset()
serializer = arduinoSerializers(queryset, many=True)
if serializer.is_valid() :
perform_update(serializer)
return Response(serializer.data)
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('api.urls')),
path('auth/', include('rest_auth.urls')),
path('auth/registration/', include('rest_auth.registration.urls')),
]
api\urls.py
urlpatterns = [
path('data/', arduinoViewSet.as_view({'get' : 'data'})),
path('create/', createViewSet.as_view()),
path('update/', updateViewSet.as_view()),
]
thanks in advance
urlpatterns = [
path('update/<int:pk>/', updateViewSet.as_view()),
]