Home > database >  Rest API: Why is object doesn't findable in 'create' function?
Rest API: Why is object doesn't findable in 'create' function?

Time:01-30

For example, here is my all data : IMG1

My viewset :

class CardCheckViewSet(viewsets.ModelViewSet):
    serializer_class = CardCheckSerializer
    queryset = CardCheck.objects.all()

    def create(self, request):
        for obj in CardCheck.objects.all():
            print(obj)

        return super().create(request)

And here is the data after I post something in it: IMG2

There should be 4 objects but this is what I got printed :

CardCheck object (30)
CardCheck object (34)
CardCheck object (44)

There is not the posted data as you see. Why is that data doesnt appears in here? What can I do about it?

CodePudding user response:

You try to print the CardCheck items before adding a new one. This way should work:

class CardCheckViewSet(viewsets.ModelViewSet):
    serializer_class = CardCheckSerializer
    queryset = CardCheck.objects.all()

    def create(self, request):
        new_obj = super().create(request)
        for obj in CardCheck.objects.all():
            print(obj)

        return new_obj
  •  Tags:  
  • Related