0

Im currently working an e-shop. So my idea is to store images with Django models in PgAdmin4. As i saw in older posts methods like bytea('D:\image.jpg') and so on just converts the string constant to its binary representation. So my question is if there is a newer method to store the actual image, or if it is possible to grab the image via a path?

models.py

image = models.ImageField(null=True, blank=True)

PgAdmin4

INSERT INTO product_images(
    id, image)
    VALUES (SERIAL, ?);// how to insert image?
narico
  • 17
  • 1
  • You should store it as an base64 string. It was asked here https://stackoverflow.com/questions/36993615/save-base64-string-into-django-imagefield – preator Nov 27 '20 at 12:24

1 Answers1

-1

There are several options for keeping images. The first is to use a storage service like S3, which I recommend. You can read this article for more detailed information. I can also recommend that I have used a third party package ready to use S3 with Django. If you use this option, imagefield will keep the path in S3.

Another option is if you are using only one server, you can keep the pictures in that server's local. Again imagefield will keep the path.

If you say I want to keep it directly in the database, you can follow this link. Currently, there is no newer method for it.

But I have to say that I think using a storage service like S3 is the best way under all circumstances.

Ogulcan Olguner
  • 571
  • 2
  • 12
  • What is wrong with storing images in a database? – tadasajon Apr 19 '23 at 21:08
  • I haven't generalized all databases and I think it's not a good idea to keep images in Postgres except in some rare cases. There are many reasons for this and it is not possible for me to detail all of them one by one. But if you are interested, you can read the article below and there are similar other articles too. https://maximorlov.com/why-storing-files-database-bad-practice/ – Ogulcan Olguner May 05 '23 at 09:23