Ticket #5485: base.py.diff
File base.py.diff, 1.5 KB (added by , 17 years ago) |
---|
-
django/db/models/base.py
380 380 pass 381 381 filename = field.get_filename(filename) 382 382 383 # If the filename already exists, keep adding an underscore to the name of383 # If the filename already exists, add an underscore and number to 384 384 # the file until the filename doesn't exist. 385 counter = 2 385 386 while os.path.exists(os.path.join(settings.MEDIA_ROOT, filename)): 386 387 try: 387 388 dot_index = filename.rindex('.') 388 389 except ValueError: # filename has no dot 389 filename += '_' 390 try: 391 number_index = filename.rindex('_') 392 except ValueError: 393 filename += '_'+str(counter) # There was no underscore 394 else: 395 filename = filename[:number_index] + '_' + str(counter) 390 396 else: 391 filename = filename[:dot_index] + '_' + filename[dot_index:] 397 try: 398 number_index = filename.rindex('_') 399 except ValueError: 400 filename = filename[:dot_index] + '_' + str(counter) + filename[dot_index:] 401 else: 402 filename = filename[:number_index] + '_' + str(counter) + filename[dot_index:] 403 counter += 1 392 404 393 405 # Write the file to disk. 394 406 setattr(self, field.attname, filename)