from __future__ import print_function import datetime import os import sys from django.conf import settings from django.db.models import get_models, FileField all_files_on_fs = set() for dirpath, dirnames, filenames in os.walk(settings.MEDIA_ROOT): rootpath, dirname = os.path.split(dirpath) all_files_on_fs |= set([os.path.join(dirname, name) for name in filenames]) all_files_on_db = {} for model in get_models(): for field in model._meta.fields: if isinstance(field, FileField): for pk, filepath in model.objects.values_list('pk', field.name): if filepath: assert filepath not in all_files_on_db all_files_on_db[filepath] = (model, pk) with open("FileField_mismatches.txt", "w") as o: print (datetime.datetime.now(), file=o) print ("\n== Summary: ==", file=o) print ("FS Count:", len(all_files_on_fs), file=o) print ("DB Count:", len(all_files_on_db), file=o) print ("\n== Referenced in the DB but not existing on FS: ==", file=o) for fn, (model, pk) in all_files_on_db.items(): if fn not in all_files_on_fs: print (model, pk, fn, file=o) print ("\n== Existing in FS but non referenced in the DB: ==", file=o) for fn in all_files_on_fs: if fn not in all_files_on_db: print (fn, file=o)