Images/files not shown after psycopg2 to psycopg3 upgrade

After upgrade from psycopg2 to psycopg3 in my Django project, my images (which I needed to store as bytea in the database) no longer render.

Consider person = Person.objects.get(pk=1). I then do FileResponse(person.picture, as_attachment=True) to get the picture and serve it to the response.

With psycopg2, type(person.picture) yielded memoryview. With psycopg3, I get bytes.

The 2 to 3 differences documentation doesn't indicate what could be wrong.

Wrap the data buffer in an iterable as such: FileResponse((person.picture, ), as_attachment=True).

This also provides backwards compatibility (i.e. works on versions 2 and 3).

See also https://github.com/psycopg/psycopg/discussions/733

This is documented in the psycopg 3.0.17 changelog https://www.psycopg.org/psycopg3/docs/news.html#psycopg-3-0-17:

Load bytea as bytes, not memoryview, using ctypes implementation.
Back to Top