Satvik
Satvik

Reputation: 137

How to perform Update, Delete operation in Django rest framework

I want to perform Update and Delete operation in Django rest framework, I did Get and Post operation. I'm new to django, Please help me to do Update and Delete operation.

views.py

class StudentViews(viewsets.ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentSerializer

models.py

class Student(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    contact_number = models.CharField(max_length=12, blank=True, null=True)
    email = models.EmailField(max_length=100, blank=True, null=True)
    address = models.CharField(max_length=500, blank=True, null=True)

serializers.py

class StudentSerializer(serializers.ModelSerializer):

    class Meta:
        model = Student
        fields = '__all__'

urls.py

router = routers.DefaultRouter()
router.register('api/student', views.StudentViews)

urlpatterns = [
    path('', include(router.urls)),
]

Upvotes: 0

Views: 569

Answers (1)

JPG
JPG

Reputation: 88669

You can do those operations(PUT,PATCH and DELETE) in the api/student/1234/ end-point, where the 1234 is the PK of the instance to be deleted or updated.

You can refer more related to the end-point which is created buy the router automatically here, DefaultRouter--[DRF-DOC]

Upvotes: 1

Related Questions