On Wed, May 13, 2009 at 8:05 AM, jai_python <[email protected]> wrote:
>
> I want to create Dynamic Upload Path for my models as follows.
>
> Models.py.
> ---------------------------------------------------------------
> class Books(models.Model):
> no=models.IntegerField()
>
> class Collection(models.Model):
> Page=models.ForeignKey('Books')
> image=models.ImageField(upload_to='Book_Media')
> def save(self):
> for field in self._meta.fields:
> if field.name == 'image':
> field.upload_to = 'Book_Media/'+str(self.Page.no)
> super(Collection, self).save()
> ---------------------------------------------------------------------
>
> Issue:
> ---------
> When ever i upload an image in 'Collection Models', the image is
> stored in previous object path.
> how can we overcome this ? Or else is there any other solution for
> DYNAMIC PATH based on object?
>
The correct way to make upload_to dynamic is to make it a callable:
http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
That way your code will be called to provide the path at the right time.
What you are doing now apparently results in your changing the field's
upload_to value after the object you are currently saving has already been
uploaded to the previous upload_to value.
Karen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---