Django Rest Framework ModelViewSet When Posted?

0

Issue

I have a simple rest application.

models.py :

class Check(models.Model):
    crypted = models.TextField()
    anid = models.IntegerField()

    def __str__(self):
        return str(self.anid)

serializers.py :

class CheckSerializer(serializers.ModelSerializer):
    class Meta:
        model = Check
        fields = ['crypted', 'anid']

views.py :

class CheckViewSet(viewsets.ModelViewSet):
    serializer_class = CheckSerializer
    queryset = Check.objects.all()

urls.py :

router = routers.DefaultRouter()
router.register('all', CheckViewSet)

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

Here is what I need : I need to make a code run "when I posted into this page"
How can I provide this?

Solution

For that you should override create method of ModelViewSet:

class CheckViewSet(viewsets.ModelViewSet):
    def create(self, request):
        # Your code here.
        return super().create(request)

Answered By – Yevgeniy Kosmak

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More