response returning no data django-rest

Viewed 627

i don't know whats wrong with this code, previously i was getting assertion error and now this

class OrganisationSerializer(ModelSerializer):
    products = ProductSerializer(many=True)   #manytomany field
    pictures = PicturesSerializer(source ='pictures_set',many=True)  #foreign key field
    class Meta:
        model = Organisation
        fields = ['name','address' ,'products','pictures']



@api_view(['GET'])
def get_data(request):
    data = None
    query_set = Organisation.objects.all()
    serialized_data = OrganisationSerializer(data = query_set,many =True)
    if serialized_data.is_valid():
        data = serialized_data.data
        print(data)
    return Response(data,status=status.HTTP_200_OK)

problem

returning no data 
1 Answers

That means that your OrganisationSerializer is not valid, but that should always be the case, since you did not bound it with request data:

@api_view(['GET'])
def get_data(request):
    queryset = Organisation.objects.all()
    serializer = OrganisationSerializer(queryset, many=True)
    return Response(serializer.data, status=status.HTTP_200_OK)

We thus use the OrganisationSerializer to convert model objects to data, not constructing Organisation objects from data from the request.

Related