For what it's worth, if all you want to do is change the upload folder, you
can also do it by overriding the form's __init__.

- Paulo

On Tue, Aug 4, 2009 at 9:28 PM, Stefan Hjelm <stefanhjel...@gmail.com>wrote:

>
> ok don't know what typo I made yesterday but it works now, maybe
> backslash in path. Thanks.
>
> for you who want to know how:
>
> def get_path(instance, filename):
>    return instance._folder
>
> My model:
> class File(models.Model):
>    type = models.CharField(max_length=10)
>     the_file = models.FileField(upload_to=get_path)
> def __unicode__(self):
>        return self.file.name
> def save(self, force_insert=False, force_update=False):
>         self._folder = "new_folder/"+self.the_file.name
>         super(File, self).save(force_insert, force_update)
>
>
> On 4 Aug, 22:02, Daniel Roseman <dan...@roseman.org.uk> wrote:
> > On Aug 4, 8:53 pm, caliman <stefanhjel...@gmail.com> wrote:
> >
> >
> >
> > > Hi!
> >
> > > I'm trying to change upload_to dynamically.
> >
> > > My model:
> > > class File(models.Model):
> > >     type = models.CharField(max_length=10)
> > >     the_file = models.FileField(upload_to="folder")
> > > def __unicode__(self):
> > >         return self.file.name
> >
> > > Tried following:
> >
> > > 1, overwrite upload_to in save:
> > > def save(self, force_insert=False, force_update=False):
> > >         the_file.upload = "new_folder"
> > >         super(File, self).save(force_insert, force_update)
> >
> > > This gives:
> > > Exception Type:         NameError
> > > Exception Value:        global name 'the_file' is not defined
> >
> > > Also tried with slug fields and setting a function and set value in
> > > runtime with:
> > > the_file = models.FileField(upload_to=get_path)
> >
> > > that works great if I just want to change the filename but I can't
> > > change folder.
> >
> > > So what am I doing wrong?
> >
> > > Stefan
> >
> > Alex has given you the answer regarding why you're getting that
> > exception, but even when you've fixed that it won't do what you want -
> > by then it's far too late, the file has already been uploaded.
> >
> > Your second approach is the right one, making upload_to a function -
> > as explained in the documentation:
> http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
> > But I don't understand why you say you can't change the folder there.
> > On the contrary, you can return a complete file path from your
> > upload_to function and that will be used as the file location. What
> > happens when you try?
> > --
> > DR
> >
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to