[Django]-Django-Rest-Framework CreateAPIView not working

5👍

If you are create an object, just POST to /api/ URL. Not /api/create/. This is because your ArticleViewSet contains CreateAPIView. If you are using like this:

from rest_framework import generics
class ArticleViewSet(generics.CreateAPIView):
    serializer_class = ArticleSerializer
    queryset = Article.objects.all()

Your routers in urls.py does not working if you use CreateAPIView. Your tutorial explains this.

You need to do send POST data /api/ URL for object creation.

  • POST method for create
  • PUT method for edit
  • GET method for list or detail
  • DELETE for delete

operations in viewsets.

If you want to use CreateAPIView(like the above code) you must change urls.py like this.

urlpatterns = [
    url("/api/create/", views.ArticleViewSet.as_view())
]

Leave a comment