I've revised my code but files are still getting uploaded to the
'listings' directory.

class Listing(models.Model):
    user = models.ForeignKey(User)
    zipfile = models.FileField(upload_to='listings')
    name = models.CharField(max_length=50)
    order = models.ForeignKey('Order')

    def overwrite_upload_to(self):
        user_dir_path = os.path.join(settings.MEDIA_ROOT, 'listings',
self.user.username)
        self.zipfile.url=user_dir_path

    def save(self, force_insert=False, force_update=False):
        Listing.overwrite_upload_to(self)
        super(Listing, self).save(force_insert, force_update) # Call
the "real" save() method.

    def __unicode__(self):
                return self.name

On Jul 22, 10:59 pm, neridaj <neri...@gmail.com> wrote:
> I'm new to django and not sure how to do this, is this remotely close?
>
> class Listing(models.Model):
>     user = models.ForeignKey(User, unique=True)
>     zipfile = models.FileField(upload_to='listings')
>     name = models.CharField(max_length=50)
>     order = models.ForeignKey('Order')
>
>     def save(self, user=user):
>         user_dir_path = os.path.join(settings.MEDIA_ROOT, 'listings',
> user)
>         zipfile.upload_to=user_dir_path
>
>     def __unicode__(self):
>                 return self.name
>
> On Jul 18, 5:20 am, Eugene Mirotin <emiro...@gmail.com> wrote:
>
> > What you have written (this join for user_dir_path) is evaluated only
> > once on model class creation, but what you need is evaluating it for
> > each specific object.
>
> > I recommend you overloading the model save method to alter the image
> > save path.
>
> > On Jul 17, 10:44 am, "neri...@gmail.com" <neri...@gmail.com> wrote:
>
> > > Hello,
>
> > > I'm trying to build a photo upload path which corresponds to the
> > > selected user i.e., when the user is selected from the drop down the
> > > username gets appended to the file upload path dynamically. If you
> > > have any ideas I would like to hear them:
>
> > > class Listing(models.Model):
> > >         user = models.ForeignKey(User, unique=True)
> > >         user_directory_path = os.path.join(settings.MEDIA_ROOT, 
> > > 'listings',
> > > user)
> > >         user_directory = models.FilePathField(path=user_directory_path,
> > > recursive=True)
> > >         photo = models.ImageField(upload_to=user_directory)
> > >         name = models.CharField(max_length=50)
> > >         order = models.ForeignKey('Order')
>
> > >         def __unicode__(self):
> > >                 return self.name
>
> > > I keep getting this error:
>
> > > AttributeError: 'ForeignKey' object has no attribute 'startswith'
--~--~---------~--~----~------------~-------~--~----~
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