On Jan 3, 2018 6:16 PM, <cherngyo...@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: https://docs.djangoproject.com/en/2.0/ref/models/fields/#binaryfield -James -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/CA%2Be%2BciX9T%2B%3Do0774kuoKiMeEU2cFTHO%2BsHQTfLuC961RUF_tuw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.