2

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
  • That means that your OrganisationSerializer is not valid, but that should always be the case, since you did not bound it with request data. Commented Oct 2, 2021 at 10:00

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i still don't understand i thought they're used to convert, object instances to native types @Willem Van
@AtifShafi: yes, but the opposite direction as well: converting primitive types to Organisation objects (so deserializing). If you use this in serialization mode, then the serializer can not validate, since validation is only done to process primitive types to model objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.