Django storing byte string for image

3,266 views
Skip to first unread message

chern...@gmail.com

unread,
Jan 3, 2018, 7:03:36 AM1/3/18
to Django users
What field should i use to store byte string so that i can later use it for ByteIO to convert it into image ?

As this is my first time doing on byte string to image, i am pretty lost on what i need to do.

I tried using BinaryField for the image model but when checking the field out on Django admin, it gave me this error
'image' cannot be specified for MyUser model form as it is a non-editable field
i also tried setting BinaryField(editable=True) but it still doesnt work.

May i have some guidance on how to store the byte string ? 

Jani Tiainen

unread,
Jan 3, 2018, 7:20:23 AM1/3/18
to django...@googlegroups.com
Hi,

In general it is considered very bad practice to store files in database.

Could you please tell why you need to store files to database instead of normal filesystem?

3.1.2018 14.03 <chern...@gmail.com> kirjoitti:
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2f787ba8-6c74-4feb-bbdf-f5473acdc835%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Avraham Serour

unread,
Jan 3, 2018, 8:06:46 AM1/3/18
to django-users
I used this project once:

It seems it uses a TextField for the file bytes.

I would expect django.contrib.postgres to implement a binary field, but it don't.

I guess you can implement your own field type for bytea, psycopg maps bytes to bytea: http://initd.org/psycopg/docs/usage.html#adaptation-of-python-values-to-sql-types

In any case I recommend to give this a second thought, the DB is not so efficient to store and serve files as the filesystem is.

Jason

unread,
Jan 3, 2018, 11:36:30 AM1/3/18
to Django users

chern...@gmail.com

unread,
Jan 3, 2018, 9:15:53 PM1/3/18
to Django users


As i am doing a django rest framework API for IOS mobile app currently, my partner told me that he will sent me a byte string for image. Then i have to convert the byte into image when a user request it.
 
I don't mean to store it into database. Its just i do not know how to do it as there isnt much post online which talk about storing byte string. 

I understand that storing byte string/images ? is a bad design, However, i do not know which field to use first. From what i know, only ImageField and FileField have the (upload_to=) save to path function. Thus i am stuck here having no idea on what field to use.

I also have another question , is storing byte string consider as storing images ? As from my understanding, byte is not image, it is only after i convert it then it becomes an image. 

Sorry for my inexperience, as this is the first time storing byte.
 

James Schneider

unread,
Jan 4, 2018, 2:52:34 AM1/4/18
to django...@googlegroups.com


On Jan 3, 2018 6:16 PM, <chern...@gmail.com> wrote:


As i am doing a django rest framework API for IOS mobile app currently, my partner told me that he will sent me a byte string for image. Then i have to convert the byte into image when a user request it.
 

Storing the file in the DB (be it an image or otherwise) is generally considered bad practice because it bloats the database, can't be used against searches, and may cause row results to be bloated if they include the column containing the file.

Having the image in the DB also makes it difficult for other programs to interact with the files. A common operation is to automatically generate thumbnail versions of images for previews of a larger image.

I don't mean to store it into database. Its just i do not know how to do it as there isnt much post online which talk about storing byte string. 

You are better off storing the file directly on the hard drive using Django's standard file fields, which simply stores the location of the file on the hard drive along with some other Meta data.


I understand that storing byte string/images ? is a bad design, However, i do not know which field to use first. From what i know, only ImageField and FileField have the (upload_to=) save to path function. Thus i am stuck here having no idea on what field to use.

You have a choice. If you store the raw byte string in a local file without converting it, use a File fields. If you plan to convert the file to a real image format before saving it, use an ImageField.

If there is no other reason to convert it and there is a chance that the file will not be accessed later (or infrequently), then you may be able to save some processing by not converting the file until it is later accessed, converting it on the fly as you serve it out. If the images are likely to be accessed later or more than once, it's probably better to convert them up front so you can perform a straight file transfer later, which will be cheaper on resources in the long run.


I also have another question , is storing byte string consider as storing images ? As from my understanding, byte is not image, it is only after i convert it then it becomes an image. 

You're likely receiving a base 64 encoded byte string (or some variant of that).

It doesn't matter what format the bytes will ultimately take on, it's still a bad idea for the reasons I outlined above.

Django does have a BinaryField if you really have your heart set on it:


-James

Shem Geek

unread,
Jan 4, 2018, 3:56:06 AM1/4/18
to Django users
Whether stored as bytestrings or images, it is a bad practice to store images in the database.
I believe from the problem description that what you are trying to acomplish is store images which you receive in the form of a bytestring.
Have you considered converting the bytestring to an image and storing the image in file system using normal imageField:
It would be something like this:

            
 import base64
 
def _construct_image(bytestring):
    img
= open("imageToSave.png", "wb")
    img
.write(str.decode('base64'))
    img
.close()




In your django models, you use this function to convert the bytestring to an image and store where upload_to
 wants to store it.
Suppose you choose to overide the save method to handle the handle the conversion, that will be something like:

def save(self, *args, **kwargs):
   
....
    image
= _construct_image(self.image)
   
....
   
return Super(YourModel, self).save(*args, **kwargs)


Check if it works.

chern...@gmail.com

unread,
Jan 4, 2018, 4:15:29 AM1/4/18
to Django users
From the requirement i get, i am suppose to store the bytestring into another db. Main db is using postgresql, the image however is save to path into mongodb.
and yes,  the image im recieving is in byte string. Thank you guys for your help ! I'm going to try it out locally first

Shem Ogumbe

unread,
Jan 4, 2018, 4:44:53 AM1/4/18
to Django users
Maybe try the conversion then save the path to MongoDb, but not the bytsetring itself


On Wednesday, January 3, 2018 at 3:03:36 PM UTC+3, chern...@gmail.com wrote:

را نيا

unread,
Aug 4, 2020, 3:02:38 PM8/4/20
to Django users
Good evening
I want to import photos and PDF files from desktop app and display them in web app, how can I do it without using binary format?

Mottaz Hegaze

unread,
Aug 5, 2020, 7:53:53 AM8/5/20
to Django users
You can use database to store the location of the file on server , not the file itself..

Checkout in Documentation

models.ImageField()
models.FileField()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/348196d3-9529-4fdc-8686-fb9b375fdbd6o%40googlegroups.com.

Andréas Kühne

unread,
Aug 5, 2020, 7:54:31 AM8/5/20
to django...@googlegroups.com
Hi,

You don't need to store them in the database - just store them in the file system instead?

Regards,

Andréas


‪Den tis 4 aug. 2020 kl 21:02 skrev ‫را نيا‬‎ <raniac...@gmail.com>:‬
--
Reply all
Reply to author
Forward
0 new messages