Django REST: CREATE function is not calling inside serialize class -
the problem create function not calling , returning validation boolean value whether true or false. please note: validation part working fine.
code follows:
views.py
class testapi(apiview): serializer_request = testapiserializer def post(self,request): obj = self.serializer_request(data=request.data) print obj.is_valid() if obj.is_valid(): obj.save() return response(status=status.http_200_ok) else: return response(request_for_demo_ser.errors, status=status.http_400_bad_request)
serializers.py
class testapiserializer(serializers.modelserializer): class meta: model = testdemo fields = ('name') # function not @ calling. def create(self, validated_data): name = validated_data['name'] return testdemo.objects.create(name=name)
you forgot save.
obj = self.serializer_request(data=request.data) print obj.is_valid() if obj.is_valid(): obj.save() # add line # change code 201 return response(status=status.http_201_created) # return response(status=status.http_200_ok)
Comments
Post a Comment