Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Maxim Andreev
Maxim Andreev
24,529 Points

DRF "must call is_valid" assertion error

Hi,

I am getting the following error:

AssertionError: When a serializer is passed a `data` keyword argument you must call `.is_valid()` before attempting to access the serialized `.data` representation.
You should either call `.is_valid()` first, or access `.initial_data` instead.

I understand that I can simply validate the serializer and it will work. However, this is a response serializer, as in I am generating a custom response object, after some foobar operations, and then am serializing it for the response.

My question is, why is is_valid still required in those cases? Shouldn't it just let me access the data attribute of the serializer without validation?

Thanks

PS here is a snippet of my code, unfortunately I had to edit it for privacy reasons:

get_range_availability -> is a custom function I run after receiving user input 'bar' (yes, I validate the user input and run it through a separate serializer.). I am validating the serializer in this example so I am of course not getting that error.

availability = get_range_availability(foo=bar)
serializer = MyCustomSerializer(data=availability)
if serializer.is_valid():
    return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

DRF's Serializer.is_valid() isn't used like Django's Form.is_valid(). You just call it, not use it in an if. Try something like

availability = get_range_availability(foo=bar)
serializer = MyCustomSerializer(data=availability)
try:
    serializer.is_valid(raise_exception=True)
except serializers.ValidationError:
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
    return Response(serializer.data, status=status.HTTP_200_OK)
Maxim Andreev
Maxim Andreev
24,529 Points

Hi,

Thank you, I'll make sure to use it this way going forward.

However my question is really not about how to fix this error. But to better understand DRF. The code I posted will actually run fine without an error, even with the if statement.

Since I am using a completely different Serializer for the response (I use a separate serializer to validate the request), isn't validating the response serializer sort of redundant?

I also tend to get quite a few "Expected a dict but received an 'Object'." errors when creating custom responses with serializers. Is that something you come upon very often?

Thanks

EDIT:

Figured out that I was doing wrong. For the response, I do not need to validate the data, hence I should not use the "data=" parameter when initializing the response serializer. And simply return "Response(serializer.data)".