On Sat, Mar 14, 2009 at 4:13 PM, Christoph Wegscheider < christoph.wegschei...@gmail.com> wrote:
> > 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 > > > 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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---