Skip to content

Instantly share code, notes, and snippets.

@abrookins
Created August 23, 2012 19:32
Show Gist options
  • Save abrookins/3440646 to your computer and use it in GitHub Desktop.
Save abrookins/3440646 to your computer and use it in GitHub Desktop.
Set the path of an ImageField in Django
from django.db import models
# Assuming this model.
class Example(models.Model):
image = models.ImageField(upload_to="somewhere/special")
# You want to set this field to point to an existing image (in a script, or a view, etc.).
example = Example.objects.get(id=1)
# You must use a path relative to the settings.MEDIA_ROOT.
example.image = 'somewhere/special/filename.jpeg'
example.save()
@Apoorva-Shukla
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment