0

I'm new to Django and databases.

Django used the sqlite database as it's default database. Is it possible to store images in the sqlite database and display it on my webpage? I can't find any documentation on the official Django website for it.

Giri Kishore
  • 21
  • 1
  • 6
  • It is not a good idea to store the image *data* in the database, since databases are rather slow to retrieve large amounts of data: it requires encoding and decoding. An [**`ImageField`**](https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ImageField) will store the filepath of the image, and store the image on the filesystem. – Willem Van Onsem Dec 27 '20 at 13:44

3 Answers3

5

Django's ImageField and FileField are both just links to the location where the file is actually uploaded. If you read Django's documentation on how to use imageField you may observe a upload_to attribute.

Both Fields are implemented in Django's ORM and are DB agnostic (i.e. should work on SQLite, MySQL or any other DB supported by Django).

You can check this out for examples on how to mange files.

The first example in the link shows a Car model and uploads the image to cars under the MEDIA_ROOT folder.

from django.db import models

class Car(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    photo = models.ImageField(upload_to='cars')

Alternative

If you really need the image to live in your database you can always utilize django's BinaryField and save the whole image as BLOB.

from django.db import models

class Car(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=5, decimal_places=2)
    photo_as_blob = models.BinaryField()  # save photo as binary object

As you can see from other answers it is not generally considered a good practice to save big files directly in DB.

Gr3at
  • 330
  • 6
  • 12
  • Thank you for your answer. If I'm trying to build an e-commerce website, where can I store images of the products? – Giri Kishore Dec 27 '20 at 14:49
  • The first approach would be the way to go, i.e. adding files in folders under the MEDIA_ROOT. Keep in mind that you can use custom functions with the ImageField's `upload_to` attribute. Check [this answer](https://stackoverflow.com/a/1190866/10269515) for more details. – Gr3at Dec 27 '20 at 15:22
  • Thanks a lot. This answer was really helpful. – Giri Kishore Dec 27 '20 at 15:41
1

SQLite is the default database system of the Django Framework. It is quite good for local servers. But, if you want to host the project in it would be better to use MySQL or PostgreSQL. My recommendation would be to use PostgreSQL. If you want to host it in Heroku, they use PostgreSQL and that is super easy. You can also use AWS, which is easy for both PostgreSQL and MySQL.

At last, my personal recommendation would be to use PostgreSQL.

Jafoor
  • 695
  • 5
  • 14
0

Generally speaking SQLite is not a great database for any serious project. You would normally use something like MongoDB or Postgres or MySQL. I think Django works quite well with PostgreSQL. SQLite is great for practice and testing or small project but once you have to scale up, you can get in trouble.

Also, like Willem said, storing images in a DB is not a good idea. Normally instances stored in a DB would have a path towards an image file stored in your computer or a key to an image stored on an image storing service like Cloudinary.

  • 1
    Thank you for your answer. If I'm trying to build an e-commerce website, where can I store images of the products? What's the best practice? – Giri Kishore Dec 27 '20 at 14:51
  • Like I said, I would try something like Cloudinary at first. It has a free option that gives you 25 GB to store images and and API that allows you to store and retrieve images using unique IDs for each image. If you get a lot of traffic and people are posting a lot of pictures, it allows you to scale up to 600 GB. I'm not very familiar with e-commerce though, so platforms like Shopify might have their own system of storing images, just look around for a bit. Bottom of the line, don't store images on your computer or the DB especially not in something like SQLite. – Ioan Andrei Dec 28 '20 at 08:29