Robert
Robert

Reputation: 75

DjangoREST using DELETE and UPDATE with serializers

So I followed the Quickstart Guide on the DjangoREST framewok site and ended up with the following code:

serializers.py:

class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
    model = User
    fields = ('url', 'username', 'email', 'groups')


class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
    model = Group
    fields = ('url', 'name')

views.py:

class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer

class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer

urls.py:

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
router.register(r'rooms', views.RoomViewSet)
router.register(r'devices', views.DeviceViewSet)
router.register(r'deviceTypes', views.DeviceTypeViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls',  namespace='rest_framework'))
]

Now this all works fine, but I cant find out how to DELETE or UPDATE a user or a group, seems I can only add users and groups and view them. So my question is: How can I modify this code to make it possible to delete/update users and groups?

Upvotes: 0

Views: 3526

Answers (3)

Shamim
Shamim

Reputation: 705

Use 'curl' or 'httpie' to perform DELETE or UPDATE or any other request.

Example:

http DELETE example.org/todos/7

or

curl -X "DELETE" http://www.url.com/page

See details here:

  1. https://httpie.org/doc

  2. https://www.garron.me/en/bits/curl-delete-request.html

Upvotes: 0

Egalicia
Egalicia

Reputation: 723

You must perform a HTTP request with method DELETE without any header and body. example

curl -X DELETE http://127.0.0.1:8000/api/v1/persona/4/

only for contrast, the create will be perform the next CURL (note the header "Content-Type"):

curl -X POST http://127.0.0.1:8000/api/v1/persona/ -H 'content-type: application/json' -d '{
"nombre": "Rick",
"apellidos": "Isenhower",
"edad": "38",
telefono": "8547458858", 
"domicilio": "St Utha, Colorado"}'

Cheers C:

Upvotes: 1

Sayse
Sayse

Reputation: 43300

The code is fine, you just need to use PUT and DELETE data methods for update and delete respectively (instead of GET/POST)

You can see from the code example for a ModelViewSet in the docs

class SnippetViewSet(viewsets.ModelViewSet):
    """
    This viewset automatically provides `list`, `create`, `retrieve`,
    `update` and `destroy` actions.

and the docs for ModelViewSet

The actions provided by the ModelViewSet class are .list(), .retrieve(), .create(), .update(), .partial_update(), and .destroy().

Upvotes: 5

Related Questions