(Django1.5, Pyhton2.7)
修正:2013-09-01
models.py
def avatar_file_name(instance, filename): return os.path.join('avatar', str(instance.user.id), 'original', filename) class Profile(models.Model): user = models.OneToOneField(User) avatar = models.ImageField(upload_to=avatar_file_name) def save(self, *args, **kwargs): try: original_profile = Profile.objects.get(pk=self.pk) if original_profile.avatar: original_profile.avatar.delete(save=False) except self.DoesNotExist: pass super(Profile, self).save(*args, **kwargs)