foreign key id as part of models.FileField(upload_to=...)

1,168 views
Skip to first unread message

Christoph Wegscheider

unread,
Mar 14, 2009, 5:13:39 PM3/14/09
to Django users
Hi,
I want to upload photos to directories structured by the album primary
key. I have the following model:

class Photo(models.Model):
album = models.ForeignKey(Album)
img = models.FileField(upload_to='albums/images/' + str(album.pk)
+ '/', blank=True)

The str(album.pk) part is not working. As I understand it I can't use
the album as an object of type album here like in views/templates (I'm
new to python and haven't fully understand the mechanisms working
underneath, but I guess the class declaration is processed earlier,
when the information is not yet available).

Nevertheless, I'm sure I can use the foreign key id for albums somehow
as part of the path, but how?
Any hint will be appreciated.

Thanks,
Christoph

Alex Gaynor

unread,
Mar 14, 2009, 5:18:57 PM3/14/09
to django...@googlegroups.com
You can do this by providing a callable to upload_to: http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield .  Using the instance, so this might look like::

def upload_location(instance, filename):
    return 'albums/images/%s/%s' % (instance.album.pk, filename)

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Dirk Eschler

unread,
Mar 14, 2009, 5:29:16 PM3/14/09
to django...@googlegroups.com

Hi Christoph,

since the FileField refactoring you can define a callable for upload_to.
Basically that's a function which knows about the instance and filename and
returns the full image path. Like this:

from django.conf import settings

def get_img_storage_path(instance, filename):
return '%salbums/images/%s/' % (settings.MEDIA_ROOT, instance.album.pk)

class Photo(models.Model):
img = models.FileField(upload_to=get_img_storage_path, blank=True)


Also see:
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.FileField.upload_to

Best Regards,
Dirk Eschler

--
Dirk Eschler <dirk.e...@gmx.net>
http://www.krusader.org

Dirk Eschler

unread,
Mar 14, 2009, 5:42:44 PM3/14/09
to django...@googlegroups.com
Am Samstag 14 März 2009 22:29:16 schrieb Dirk Eschler:
> def get_img_storage_path(instance, filename):
> return '%salbums/images/%s/' % (settings.MEDIA_ROOT, instance.album.pk)

Forgot the filename in my example...

def get_img_storage_path(instance, filename):
return '%salbums/images/%s/%s' % (settings.MEDIA_ROOT, instance.album.pk,
filename)

Christoph Wegscheider

unread,
Mar 14, 2009, 5:55:59 PM3/14/09
to Django users
> Dirk Eschler <dirk.esch...@gmx.net>http://www.krusader.org

Hi Dirk,
exactly the solution I was looking for. Thank you (and Dirk) very
much.

I tried it meanwhile and stumbled over the filename myself :),
additionally I omitted the MEDIA_ROOT part as it seems to be done
automatically. If I use it, the img.url output is wrong (shows the
absolute path instead of the relative one).

Christoph
Reply all
Reply to author
Forward
0 new messages