I getting null in return when in fact i have data in the database, I don't where the problem lies, pretty frustrated by this, if I am not calling is_valid then it throws an assertion error but anyways data is still empty
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
View
@api_view(['GET'])
def get_data(request):
product_data = Product.objects.all()
print(product_data)
serialized_data = ProductSerializer(data = product_data, many =True)
data = {}
if serialized_data.is_valid():
data['status']='valid'
else:
data['status']='not valid'
return Response(data)
CodePudding user response:
You were using the DRF serializer in the wrong way, try to use the instance
argument, instead of data
while serializing the data.
@api_view(['GET'])
def get_data(request):
product_qs = Product.objects.all()
serializer = ProductSerializer(instance=product_qs, many=True)
return Response(serializer.data)