0

I have some images i retrieve from http://image.eveonline.com/ that i want to save. Now part of those will be static, while others might change. Instead of pulling all static images from their site, i want to do this when they are needed (if they are not saved yet)

How can i tell my model to save the imagefield in static or in media

Example model:

class ItemIcon(models.Model):
    item = models.ForeignKey("items.Item")
    size = models.IntegerField(choices=settings.IMAGE_SIZES)
    url = models.URLField(unique=True) #The url to the evesite
    image = models.ImageField(upload_to="static/images/items/")

(this model should be saved in static)

levi
  • 22,001
  • 7
  • 73
  • 74
Hans de Jong
  • 2,030
  • 6
  • 35
  • 55

1 Answers1

4

you need to set your MEDIA_URL and MEDIA_ROOT in your settings.py and then define your image field like this:

image = models.ImageField(upload_to="items")

Note that you dont need to add the whole path to upload_to attribute, Django will append the absolute path for you. And you need to have a folder named items inside your media folder.

If you want to save images in STATIC_ROOT, try this:

from django.conf import settings

fs = FileSystemStorage(location=settings.STATIC_ROOT)

class ItemIcon(models.Model):
   item = models.ForeignKey("items.Item")
   size = models.IntegerField(choices=settings.IMAGE_SIZES)
   url = models.URLField(unique=True) #The url to the evesite
   image = models.ImageField(upload_to="items",storage=fs)
levi
  • 22,001
  • 7
  • 73
  • 74