2009/7/2 Andrew Turner <[email protected]>:
> 2009/7/2 Andrew Turner <[email protected]>:
>> Would it be possible to define the hashed filename in the save method,
>> and then pass it to the upload_to argument of the ImageField?
>
> In answer to my own question, this seems to work:-
>
> def get_path(instance, name):
> return instance._my_filename
>
> class Photo(models.Model):
> photo = models.ImageField(upload_to=get_path, blank=False)
> ...
> slug = models.CharField(max_length=16, unique=True)
>
> def save(self):
> name = hashlib.sha1(str(random.random())).hexdigest()[:16]
> self._my_filename = '%s.jpg' % (name)
> self.slug = name
> super(Post, self).save()
Talking to myself again, I've changed the save method slightly:-
def save(self):
if not self.slug:
name = hashlib.sha1(str(random.random())).hexdigest()[:16]
self._my_filename = '%s.jpg' % (name)
self.slug = name
super(Post, self).save()
and added a blank=True to the slug field so that it only sets it on
the first save.
If there is a better way of doing all this, feel free to let me know.
If not, I hope this is of use to somebody.
Thanks for your pointers, Rajesh.
Cheers,
Andrew
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---