How can RetrieveUpdateDestroyAPIView supports POST?

I successfully implement Rest API to store IP address and Mac address mapping.

The GET/PATCH/DELETE methods are supported, for example:

curl -X GET -H 'Content-Type: application/json' -d '{"ip_address": "192.168.1.1"}'  http://127.0.0.1:8000/api/ipmac/
curl -X PATCH -H 'Content-Type: application/json' -d '{"ip_address": "192.168.1.1", "mac_address": "aa:bb:cc:dd:ee:ff"}'  http://127.0.0.1:8000/api/ipmac/

However, POST is not supported in RetrieveUpdateDestroyAPIView. I don't wanna create another endpoint for POST method. How to have only one endpoint and support all the methods?

Here is my code: models.py

from django.db import models

class IPMacMapping(models.Model):
    ip_address = models.GenericIPAddressField(unique=True)
    mac_address = models.CharField(max_length=17, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)

serializers.py

from rest_framework import serializers
from .models import IPMacMapping

class IPMacMappingSerializer(serializers.ModelSerializer):
    class Meta:
        model = IPMacMapping
        fields = '__all__'

urls.py

from django.urls import path
from .views import IPMacMappingRetrieveUpdateDestroy

urlpatterns = [
    path('ipmac/', IPMacMappingRetrieveUpdateDestroy.as_view(), name='ipmac-list-create')
]

views:

from rest_framework import generics
from .models import IPMacMapping
from .serializers import IPMacMappingSerializer
from django.shortcuts import get_object_or_404

class IPMacMappingRetrieveUpdateDestroy(generics.RetrieveUpdateDestroyAPIView):
    queryset = IPMacMapping.objects.all()
    serializer_class = IPMacMappingSerializer
    lookup_url_kwarg = "ip_address"

    def get_object(self):
        result = get_object_or_404(IPMacMapping, ip_address=self.request.data["ip_address"])
        return result
Back to Top