[Fixed]-Django rest framework SerializerMethodField bizarre behaviour

0๐Ÿ‘

โœ…

It appears that DRFโ€™s ImageField returns full URL if request has been provided to serializer context, so there is no need for explicit URL construction. Clean and elegant (requires source studying though โ€“ official documentation does not mention this)

๐Ÿ‘คMike Doudkin

1๐Ÿ‘

I kinda solved it like this:

changed

if instance.image:

to

if instance.image and self.request:

so these under-the-hood get_image() calls without a proper self.request do not cause crashes anymore.

Still, it would be great to know why all of the above takes place

๐Ÿ‘คMike Doudkin

Leave a comment