def handle(self, *args, **options): if settings.MEDIA_ROOT == '': print("MEDIA_ROOT is not set, nothing to do") return # Get a list of all files under MEDIA_ROOT media = [] for root, dirs, files in os.walk(settings.MEDIA_ROOT): for f in files: media.append(os.path.abspath(os.path.join(root, f))) # Get list of all fields (value) for each model (key) # that is a FileField or subclass of a FileField model_dict = defaultdict(list) for app in get_apps_from_cache(): for model in get_models_from_cache(app): for field in model._meta.fields: if issubclass(field.__class__, models.FileField): model_dict[model].append(field) # Get a list of all files referenced in the database referenced = [] for model in model_dict: all = model.objects.all().iterator() for object in all: for field in model_dict[model]: target_file = getattr(object, field.name) if target_file: referenced.append(os.path.abspath(target_file.path)) # Print each file in MEDIA_ROOT that is not referenced in the database for m in media: if m not in referenced: print(m)
def handle_noargs(self, **options): if settings.MEDIA_ROOT == '': print("MEDIA_ROOT is not set, nothing to do") return # Get a list of all files under MEDIA_ROOT media = [] for root, dirs, files in os.walk(settings.MEDIA_ROOT): for f in files: media.append(os.path.abspath(os.path.join(root, f))) # Get list of all fields (value) for each model (key) # that is a FileField or subclass of a FileField model_dict = defaultdict(list) for app in get_apps_from_cache(): for model in get_models_from_cache(app): for field in model._meta.fields: if issubclass(field.__class__, models.FileField): model_dict[model].append(field) # Get a list of all files referenced in the database referenced = [] for model in model_dict: all = model.objects.all().iterator() for object in all: for field in model_dict[model]: target_file = getattr(object, field.name) if target_file: referenced.append(os.path.abspath(target_file.path)) # Print each file in MEDIA_ROOT that is not referenced in the database for m in media: if m not in referenced: print(m)
def __iter__(self): for model in get_models_from_cache(self.app): admin_model = AdminModel(model, **self.options) for model_re in self.model_res: if model_re.search(admin_model.name): break else: if self.model_res: continue yield admin_model