Skip to content

Instantly share code, notes, and snippets.

@maxp
Created September 7, 2009 05:44
Show Gist options
  • Save maxp/182169 to your computer and use it in GitHub Desktop.
Save maxp/182169 to your computer and use it in GitHub Desktop.
import os
from django.db import models
def get_image_path(instance, filename):
return os.path.join('photos', instance.id, filename)
class Photo(models.Model):
image = models.ImageField(upload_to=get_image_path)
if request.method == 'POST':
form = PhotoForm(request.POST, request.FILES)
if form.is_valid():
photo = Photo.objects.create()
image_file = request.FILES['image']
photo.image.save(image_file.name, image_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment