2

I use django REST framework. In my views.py has a class:

class TaskViewSet(viewsets.ModelViewSet):

    queryset = Task.objects.all().order_by('-date_created')
    serializer_class = TaskSerializer
    print("leo test in TaskViewSet")

I want to print("leo test in TaskViewSet") when I call the url every time.

But it will only print the 1 time after I use runserver cmd.

Anyone know how to print("leo test in TaskViewSet") every time when I call the api url.

My urls.py:

from django.conf.urls import url,include
from django.contrib import admin
from rest_framework import routers
from trips.views import TaskViewSet


router = routers.DefaultRouter()
router.register(r'task',TaskViewSet)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    #url(r'^$', hello_world),
    url(r'^', include(router.urls)),
]

Thank you very much.

1
  • If you use class based views you need to use get/post/put/delete methods in those classes. Then you need to add print into method. Now it is called once while it is called on class innitialization only which happens on server start. django-rest-framework.org/api-guide/views Commented Apr 6, 2017 at 7:45

1 Answer 1

4

You have to define actions to get the desired behavior. By default, the router you have defined bound to several actions like create, retrieve, list, update and destroy. So when you implement those actions via methods as below you would be able to print on every endpoint call.

class TaskViewSet(viewsets.ModelViewSet):

    queryset = Task.objects.all().order_by('-date_created')
    serializer_class = TaskSerializer
    print("leo test in TaskViewSet")

    def list(self, request):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)

    def create(self, request):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def retrieve(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def update(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def partial_update(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)


    def destroy(self, request, pk=None):
        print("leo test in TaskViewSet")
        return Response("leo test in TaskViewSet", status=status.HTTP_404_NOT_FOUND)

Let me know if something is not clear. Also please read here for more details http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.