Am Samstag 14 März 2009 22:13:39 schrieb Christoph Wegscheider: > 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.
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.esch...@gmx.net> http://www.krusader.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---