2009/6/30 Andrew Turner <acturne...@gmail.com>: > Hi, > > This is an issue which, I believe, is related to Ticket #10788 > (http://code.djangoproject.com/ticket/10788). > > The following code snippet used to work with Django 1.0.2, but with > the latest svn version the slug is not being set to the filename > produced by get_path because, as I understand it, get_path will not be > called until the instance is saved to the database. > > def get_path(instance, filename): > salt = hashlib.sha1(str(random.random())).hexdigest() > name = hashlib.sha1(salt+filename).hexdigest()[:16] > return '%s.jpg' % (name) > > class Photo(models.Model): > photo = models.ImageField(upload_to=get_path, blank=False) > ... > slug = models.CharField(max_length=16, unique=True) > > def save(self): > self.slug = self.photo.url.split('/')[-1:][0].split('.')[0] > super(Photo, self).save() > > I could call instance.save() twice in my view to force it to use the > correct name, but this feels a bit messy to me. What's the best way to > get the correct value for the slug field prior to saving it to the > database, given that the filename is 'randomly' generated?
e.g. Generated filename:- ebc151e5aad64f4b.jpg Derived url for Photo view:- http://mydomain.com/photo/ebc151e5aad64f4b >From reading the above mentioned ticket and it's referenced discussion threads, I can't see any way of generating a slug field from the filename before the instance is saved. At the moment I am saving the instance to generate the hashed filename, creating the slug based on the dynamically generated filename, and then saving to the database again. Doesn't seem ideal to me, especially as it worked without the extra save in 1.0.2, and I'm using this dynamic filename/slug method on several projects. 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 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 -~----------~----~----~----~------~----~------~--~---