# Displaying images from a DB where they're stored as binary data

**URL:** <https://forum.djangoproject.com/t/displaying-images-from-a-db-where-theyre-stored-as-binary-data/11672>\
**Category:** Using Django\
**Created:** [January 19, 2022, 1:35am UTC](https://forum.djangoproject.com/t/displaying-images-from-a-db-where-theyre-stored-as-binary-data/11672 "2022-01-19T01:35:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MSoup](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/msoup/32/6957_2.png) [@MSoup](https://forum.djangoproject.com/u/MSoup)\
**Post date:** [January 19, 2022, 1:35am UTC](https://forum.djangoproject.com/t/displaying-images-from-a-db-where-theyre-stored-as-binary-data/11672/1 "2022-01-19T01:35:20Z")

</div>

I have a DB where there are images stored as what I believe to be binary data (the SQL datatype says varbinary(max)). I ultimately want to be able to query these images and send them to the front end.

With django+djangorestframework, is there a clean way to show the images?

My model:

```auto
class Graphimages(models.Model):
  clientid ...
  graphimage = models.BinaryField(...)

  class Meta:
      managed = False
      db_table = 'GraphImages'
      unique_together = (('clientid', 'machinenumberid', 'measurementitemid', 'csvfilenamedate', 'graphtype'),)

```

My hunch is I should convert the binary data in my serializers to say, base64, then pass the base64 strings as a json response for the front end to deal with.

Is my below pseudocode a reasonable approach, or is there a better django best practices?

```auto
import base64
from someModule import someModel

model = someModel.objects.get(pk=1)

encoded = base64.b64encode(b'model.graphImage')
return jsonresponse(encoded)

```

I may have also done a bit too much abstraction and I’ve lost track of what I need to override in order to make this happen 😅

```auto
class GraphImagesSerializer(serializers.ModelSerializer):

    class Meta:
        model = Graphimages
        fields = ['clientid', 'graphimage']

```

I assume DRF already handles most of the serialization for me when I use serializers.ModelSerializer, but I suppose I will need to start looking into how to override serialization of `graphimage`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 19, 2022, 1:46am UTC](https://forum.djangoproject.com/t/displaying-images-from-a-db-where-theyre-stored-as-binary-data/11672/2 "2022-01-19T01:46:19Z")

</div>

Part of the problem is going to be identifying what that binary data _is_ to be displayed. Even if you’re reasonably sure they’re all images, you may need to determine if they’re GIF, JPEG, PNG, or something else.

But yes, assuming your front end is going to know what to do with it, sending it as base64 is about as good as it’s going to get.
