New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cleanup command #19
Comments
I agree! |
+1 |
This might help. I made it unrelated to this issue. It's a command that lists all from django.core.management.base import BaseCommand
from django.apps import apps
from django.db import models
class Command(BaseCommand):
help = 'List all FieldFields in models'
def handle(self, *args, **options):
for model in apps.get_models():
for field_name in model._meta.get_all_field_names():
field = model._meta.get_field(field_name)
if isinstance(field, models.FileField):
print(model.__name__ + ':: ' + str(field_name) + ':\t' + type(field).__name__) If someone codes this, that code should be usable for |
@beruic, this only finds FileFields in all models. The Command must do the following:
Also, keeping in mind that a few FileFiles can share the same folder, so you must check agains all of them before removing. |
I know that :) As I wrote "This might help", but it's not the solution. I don't have the time to code it now, so I thought it better to throw my code here so someone else might benefit from it :) |
OK, actually the code is there already: https://github.com/un1t/django-cleanup/blob/master/django_cleanup/models.py#L90 |
That's a good way to do it, but perhaps the generator should be refactored to be generally available? Edit: Fixed error in code def find_models_with_filefield():
for model in get_models():
opts = ensure_get_fields(model._meta)
name = get_model_name(model)
for field in opts.get_fields():
if isinstance(field, models.FileField) and len(FIELDS[name]) == 0:
FIELDS[name].append(field.name)
if name in FIELDS:
yield model
def connect_signals():
if len(FIELDS) > 0:
return
for model in find_models_with_filefield():
key = '{{}}_django_cleanup_{}'.format(get_model_name(model))
post_init.connect(cache_original_post_init, sender=model,
dispatch_uid=key.format('post_init'))
post_save.connect(delete_old_post_save, sender=model,
dispatch_uid=key.format('post_save'))
post_delete.connect(delete_all_post_delete, sender=model,
dispatch_uid=key.format('post_delete')) |
clean_media.py command |
👍 |
https://github.com/ledil/django-orphaned - You can also take ideas from this application |
+1 |
Is there anything I need to do when I first install this into an existing project? I'm not worried about it cleaning up orphans, but it does not seem to be doing anything when I set an existing (pre-installation) ImageField that had an image to None. The file remains in /media. |
@lanshark please open a new issue with details on what steps you have taken so far, as well as details on the configuration of the ImageField. And any details on third-party or custom fields or storage classes that you may be using. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
It would be very nice to have a command that iterate over all file fields an delete all media-files (under a path specified by a setting) that are not used by any model-field anymore.
This would be very useful for existing projects where django-cleanup was not installed since the beginning.
The text was updated successfully, but these errors were encountered: