On Wednesday, January 3, 2018 at 3:03:36 PM UTC+3, [email protected] wrote:
>
> 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 ?
>
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.
--
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 [email protected].
To post to this group, send email to [email protected].
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/63d4b946-3b58-44c6-94c1-f61c9a91abaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.