23👍
You’ll have to delete the old image manually.
The absolute path to the image is stored in your_image_field.path
. So you’d do something like:
os.remove(your_image_field.path)
But, as a convenience, you can use the associated FieldFile object, which gives easy access to the underlying file, as well as providing a few convenience methods. See http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield-and-fieldfile
80👍
Use django-cleanup
pip install django-cleanup
settings.py
INSTALLED_APPS = (
...
'django_cleanup.apps.CleanupConfig', # should be placed after your apps
)
- [Django]-Select_related with reverse foreign keys
- [Django]-ValueError: Missing staticfiles manifest entry for 'favicon.ico'
- [Django]-What is the purpose of NGINX and Gunicorn running in parallel?
22👍
Use this custom save method in your model:
def save(self, *args, **kwargs):
try:
this = MyModelName.objects.get(id=self.id)
if this.MyImageFieldName != self.MyImageFieldName:
this.MyImageFieldName.delete()
except: pass
super(MyModelName, self).save(*args, **kwargs)
It works for me on my site. This problem was bothering me as well and I didn’t want to make a cleanup script instead over good bookkeeping in the first place. Let me know if there are any problems with it.
- [Django]-How do I prevent fixtures from conflicting with django post_save signal code?
- [Django]-How to compare two JSON objects with the same elements in a different order equal?
- [Django]-Manage.py runserver
19👍
Before updating the model instance, you can use the delete method of FileField
object. For example, if the FileField
or ImageField
is named as photo
and your model instance is profile
, then the following will remove the file from disk
profile.photo.delete(False)
For more clarification, here is the django doc
- [Django]-How to strip html/javascript from text input in django
- [Django]-How do I restart celery workers gracefully?
- [Django]-"Post Image data using POSTMAN"
10👍
You can define pre_save
reciever in models:
@receiver(models.signals.pre_save, sender=UserAccount)
def delete_file_on_change_extension(sender, instance, **kwargs):
if instance.pk:
try:
old_avatar = UserAccount.objects.get(pk=instance.pk).avatar
except UserAccount.DoesNotExist:
return
else:
new_avatar = instance.avatar
if old_avatar and old_avatar.url != new_avatar.url:
old_avatar.delete(save=False)
My avatrs has unique url for each person like “avatars/ceb47779-8833-4719-8711-6f4e5cabb2b2.png”. If user upload new image with different extension like jpg, delete_file_on_change_extension reciever remove old image, before save new with url “avatars/ceb47779-8833-4719-8711-6f4e5cabb2b2.jpg” (in this case). If user uploads new image with same extension django overwrite old image on storage (disk), because images paths are the same.
This works fine with AWS S3 django-storage.
- [Django]-Why is assertDictEqual needed if dicts can be compared by `==`?
- [Django]-Best way to write an image to a Django HttpResponse()
- [Django]-Unit Testing a Django Form with a FileField
4👍
Here is an app that deletes orphan files by default: django-smartfields.
It will remove files whenever:
- field value was replaced with a new one (either uploaded or set manually)
- field is cleared through the form (in case that field is not required, of course)
- the model instance itself containing the field is deleted.
It is possible to turn that cleanup feature off using an argument: ImageField(keep_orphans=True)
on per field basis, or globally in settings SMARTFIELDS_KEEP_ORPHANS = True
.
from django.db import models
from smartfields import fields
class MyModel(models.Model):
image = fields.ImageField()
document = fields.FileField()
- [Django]-Error: Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Form field description in django admin
1👍
try this, it will work even if old file is deleted
def logo_file(instance, filename):
try:
this = business.objects.get(id=instance.id)
if this.logo is not None:
path = "%s" % (this.logo)
os.remove(path)
finally:
pass..
code will work even without “try .. finally” but it will generate problem if file was accidently deleted.
changed: move model matching inside “try” so it will not throw any error at user signup
Let me know if there are any problems.
- [Django]-How to run this code in django template
- [Django]-Celery. Decrease number of processes
- [Django]-Django TypeError: render() got an unexpected keyword argument 'renderer'
0👍
Completing Chris Lawlor’s answer, tried this and works.
from YOURAPP.settings import BASE_DIR
try:
os.remove(BASE_DIR + user.userprofile.avatarURL)
except Exception as e:
pass
The URL has a pattern of /media/mypicture.jpg
- [Django]-Django: add image in an ImageField from image url
- [Django]-How do I execute raw SQL in a django migration
- [Django]-How to set and get cookies in Django?
0👍
What I did is saving the path to the old image and if form is valid I would delete the old one.
if request.method == 'POST':
old_image = ""
if request.user.profile.profile_picture:
old_image = request.user.profile.profile_picture.path
form = UpdateProfileForm(request.POST,request.FILES,instance = profile)
if form.is_valid():
if os.path.exists(old_image):
os.remove(old_image)
form.save()
It is a little messy , but you do not install third parties or anythin
- [Django]-Convert seconds to hh:mm:ss in Python
- [Django]-Django Selective Dumpdata
- [Django]-Django, creating a custom 500/404 error page