2

Running: Windows 7, Python 3.3, Django 1.6

I'm confused as to how to store an image as part of a table in a Django database. There is a field called ImageField, and here are the Docs.

Question:

What should I pass to the ImageField when constructing the record: a filepath string? A url to an image? Or a byte representation of the image itself? Or is there some way of loading an image into Django and then just passing it a reference (or pointer) to that image object?

The docs say: "By default, ImageField instances are created as varchar(100) columns in your database.", which leads me to think that it wants a file path since it's asking for a string.

The Docs also say that it "Requires the Pillow library.", which I've downloaded but it doesn't specify how to use Pillow for this.

Background: I'm building a REST API through django to send and recieve log files from Selenium tests and store them in a SQL database. Each log file comes with a screenshot, hence the need for an ImageField. Just started using Django a few days ago.

AllTradesJack
  • 2,762
  • 5
  • 25
  • 36

1 Answers1

4

Via imagefield and imagefield-derived forms you upload image to your MEDIA_ROOT.

Imagefield store its a path to image. Check the 'media' part of django documentation, it describes how to store user uploaded images for example.

Imagefield also define some basic proprieties of image like width and height.

In your setting.py file you set:

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Which said I want store uploaded files in yourproject/media and I want too show them in www.yoursite.com/media/path_to_image/image.png

Then you can define a class with imagefield.

class YourClass(models.Model):
    description = models.CharField(max_length=300, unique=True)
    picture = models.ImageField(upload_to='myimages')

So images will be stored in yourproject/media/myimages and availible at www.yoursite.com/media/myimages/image.png

To create new instance:

 from django.core.files import File
 from idontknow import YourClass

 yournewclass = YourClass(description='abc')

 yournewclass.picture.save('name.png',  File(open('path_to_pic/image.png', 'r'))
Matúš Bartko
  • 2,425
  • 2
  • 30
  • 42
  • I didn't find any "media" section here: https://docs.djangoproject.com/en/1.6/contents/ , but I did find this small paragraph: https://docs.djangoproject.com/en/1.6/faq/usage/#how-do-i-use-image-and-file-fields Is that what you meant? – AllTradesJack Jul 22 '14 at 21:38
  • Thanks for the answer. What I'm more looking for though, is when I insert a new record into the `YourClass` table, what kind of data type do I provide in that constructor? e.g. `y = YourClass(description='hello', picture= )` I apologize if I wasn't clear in my question – AllTradesJack Jul 22 '14 at 23:13
  • another edit :) I never tried this so this is my source http://stackoverflow.com/questions/15332086/saving-image-file-through-django-shell – Matúš Bartko Jul 22 '14 at 23:51
  • 1
    **edit, the mode should be `'rb'` so Python will read it as a binary file. – AllTradesJack Aug 01 '14 at 00:18