{"action":"created","issue":{"url":"https://api.github.com/repos/zacharyvoase/django-castor/issues/3","labels_url":"https://api.github.com/repos/zacharyvoase/django-castor/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/zacharyvoase/django-castor/issues/3/comments","events_url":"https://api.github.com/repos/zacharyvoase/django-castor/issues/3/events","html_url":"https://github.com/zacharyvoase/django-castor/issues/3","id":110467707,"number":3,"title":"How to implement reference counting or garbage collection","user":{"login":"beruic","id":724716,"avatar_url":"https://avatars.githubusercontent.com/u/724716?v=3","gravatar_id":"","url":"https://api.github.com/users/beruic","html_url":"https://github.com/beruic","followers_url":"https://api.github.com/users/beruic/followers","following_url":"https://api.github.com/users/beruic/following{/other_user}","gists_url":"https://api.github.com/users/beruic/gists{/gist_id}","starred_url":"https://api.github.com/users/beruic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beruic/subscriptions","organizations_url":"https://api.github.com/users/beruic/orgs","repos_url":"https://api.github.com/users/beruic/repos","events_url":"https://api.github.com/users/beruic/events{/privacy}","received_events_url":"https://api.github.com/users/beruic/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-08T14:46:58Z","updated_at":"2015-10-09T10:28:00Z","closed_at":null,"body":"I like the looks of **django-castor** and consider using it in a project. However reading the caveats section of the *README*, I was wondering how difficult it would be to let **django-castor** have automatic reference counting? This would be my preferred solution to cleaning the storage.\r\n\r\nI have been playing with the idea to implement two fields (names could be `CastorFileField` and `CastorImageField`) for the purpose, with a simple model as backing store for the counting, but I deemed this an inflexible solution because it would make it impossible to use castor for other fields that inherit from `FileField` or `ImageField`.\r\n\r\nMy next idea is to make **django-castor** store which models have saved files through the storage, and then make `delete` count from the database with these model references. Since the penalty is only big at delete time(at save time there's no counting, only addition of model reference if no already exists), I find it a promising design. A setting could be used to enable/disable this behaviour. However I currently see no way to let the storage know the model at save time. Perhaps `__init__` or `get_available_name` could read it from it's caller?\r\n\r\nThis is all unfortunately out of my scope for now, and I'm therefore wondering how I can implement garbage collection. Thinking of it, being able to garbage collect a single file, would also make it possible to clean up upon deletion.\r\n\r\nDoes anyone have an idea how to achieve clean up without coding it manually to check each of your models using the **django-castor** storage?\r\nPerhaps inspecting all models at `__init__` time?"},"comment":{"url":"https://api.github.com/repos/zacharyvoase/django-castor/issues/comments/146828067","html_url":"https://github.com/zacharyvoase/django-castor/issues/3#issuecomment-146828067","issue_url":"https://api.github.com/repos/zacharyvoase/django-castor/issues/3","id":146828067,"user":{"login":"beruic","id":724716,"avatar_url":"https://avatars.githubusercontent.com/u/724716?v=3","gravatar_id":"","url":"https://api.github.com/users/beruic","html_url":"https://github.com/beruic","followers_url":"https://api.github.com/users/beruic/followers","following_url":"https://api.github.com/users/beruic/following{/other_user}","gists_url":"https://api.github.com/users/beruic/gists{/gist_id}","starred_url":"https://api.github.com/users/beruic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beruic/subscriptions","organizations_url":"https://api.github.com/users/beruic/orgs","repos_url":"https://api.github.com/users/beruic/repos","events_url":"https://api.github.com/users/beruic/events{/privacy}","received_events_url":"https://api.github.com/users/beruic/received_events","type":"User","site_admin":false},"created_at":"2015-10-09T10:28:00Z","updated_at":"2015-10-09T10:28:00Z","body":"Heureka!\r\n\r\nI have found a way to detect models that use CAS storage. Here used in a command:\r\n\r\n```python\r\nfrom django.core.management.base import BaseCommand\r\nfrom django.apps import apps\r\nfrom django.db import models\r\nfrom djcastor import CAStorage\r\n\r\n\r\nclass Command(BaseCommand):\r\n    help = 'List all FieldFields in models, and mark the ones using CAStorage with a *'\r\n\r\n    def handle(self, *args, **options):\r\n\r\n        for model in apps.get_models():\r\n\r\n            for field_name in model._meta.get_all_field_names():\r\n                field = model._meta.get_field(field_name)\r\n\r\n                if isinstance(field, models.FileField):\r\n                    if isinstance(field.storage, CAStorage):\r\n                        print('* ', end='')\r\n                    else:\r\n                        print('  ', end='')\r\n                    print(model.__name__ + ':: ' + str(field_name) + ':\\t' + type(field).__name__)\r\n\r\n```"}}