oliver.lavery wrote: > > class Photo( Model ) > def save( self ): > c = Photo.objects.all().filter( name = self.name ).count() > if c > 0: > self.name += '_' + str( c + 1 ) > Model.save( self )
This won't work as you expect. The first file named 'foo' will be saved as 'foo'. The second as 'foo_2', but the third will still only return a count of 1 (since you are doing an exact match of the name) and you'll attempt to save it as 'foo_2' also. You could try changing the query line to something like: c = Photo.objects.filter(name__startswith=self.name).count() but that's not really right either since you'd find matches if self.name was a prefix of another photo. You might just want to do an os.listdir() to be certain. Something like: i, name = 1, self.name files = [f for f in os.listdir(SAVE_DIR) if f.startswith(self.name)] while name in files: name = self.name + '_' + str(i) i = i + 1 self.name = name -Dave --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---