Aravind Manoj
Aravind Manoj

Reputation: 1

Expected view Client_view to be called with a URL keyword argument named "pk". Fix your URL conf

[Error][1]
AssertionError at /api/client-details/ Expected view Client_view to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly. Request Method: DELETE Request URL: 
http://127.0.0.1:8000/api/client-details/ Django Version: 2.2.6 Python Executable: C:\Users\AravindManoj\PycharmProjects\Client\venv\Scripts\python.exe Python Version: 3.7.4 Python Path: 
['C:\\Users\\AravindManoj\\PycharmProjects\\Client\\Client',
'C:\\Users\\AravindManoj\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 

While using Generic view in Django it Showing Error while using DELETE function.Please anyone give me the syntax of generic views in model set i didnt find any problem but no Delete function

Views.py

    from django.http import Http404, HttpResponse
    from rest_framework import viewsets, status, generics
    from rest_framework.decorators import api_view
    from rest_framework.response import Response
    from .serializers import ClassSerializer
    from .models import Client

    class Client_view(viewsets.ModelViewSet, generics.RetrieveUpdateDestroyAPIView):
        queryset = Client.objects.all().order_by('-Name')
        serializer_class = ClassSerializer*
        lookup_fields = ['Name', 'UserName', 'Mobile', 'Email', 'Address']

urls.py

    from rest_framework import routers
    from .views import Client_view
    router = routers.DefaultRouter()
    router.register('', Client_view)
    urlpatterns = router.urls

models.py

    from django.db import models
    class Client(models.Model):
        Name = models.CharField(max_length=15)
        UserName = models.CharField(max_length=15)
        Email = models.CharField(max_length=20)
        Mobile = models.CharField(max_length=10)
        Address = models.CharField(max_length=20)

serializer.py

    from rest_framework import serializers
    from .models import Client
    class ClassSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = Client
            fields = ['Name', 'UserName', 'Email', 'Mobile', 'Address']

Upvotes: 0

Views: 4205

Answers (3)

wilson irungu
wilson irungu

Reputation: 77

just add lookup_field = 'pk' on the Client_view...

ie:

class Client_view(viewsets.ModelViewSet, generics.RetrieveUpdateDestroyAPIView):
        queryset = Client.objects.all().order_by('-Name')
        serializer_class = ClassSerializer*
        lookup_field = 'pk'

Upvotes: 0

Matt Seymour
Matt Seymour

Reputation: 9415

When using a ViewSet (in your case you are using a ModelViewSet) the view requires a pk value from urls. This is done automatically when using a router.

The router will create a number of urls: Example:

router.register(r'', Client_view)

URL pattern: ^$ Name: 'client_view-list'
URL pattern: ^{pk}/$ Name: 'client_view-detail'

One of the attributes of a ViewSet is lookup_field, the lookup field allows you to change the name of the url capture you are using. In your case, you seem to have lookup_fields and specified multiple values. Try changing this to lookup_field='pk.

As a side note, it can be helpful to take a look at the restframework source code around Views.

Upvotes: 1

Ali Asgari
Ali Asgari

Reputation: 850

You have used lookup_fields which is not any built-in feature. Try to do it with a custom mixin like described here in the docs.

Upvotes: 1

Related Questions