Hello, I'm working on a project that has specific, but not terribly complex, file management needs and I think I may need to write a custom Storage backend. But I'm not totally sure, so let me explain my problem before I ask any specific questions.
I am migrating a large set of files into this new Django project and I need to have these files private (ie. not in MEDIA_ROOT, and with url() not implemented). The file metadata such as name, md5, the uploading user, file size, etc. need to be maintained in the database, presumably computed on the fly on saving the file. The scheme used in the current system, which is so-so but I'm ok with perpetuating, is to have the files stored as (file_id % 100)/file_id, so that file_id==12345 would be stored in 45/12345. Disregarding the lack of merit in such a scheme, let's just take that as part of the exercise I'm trying to achieve here.
I have already implemented a mediocre solution with just building a file metadata model, having one of the attributes be a FileField, and overriding save() to manage the files properly, but that feels really clunky.
The documentation is pretty unhelpful in understanding when my custom Storage class really comes into play, if I should be subclassing File, how this all interacts with FileField, or if the solution lies elsewhere. For example, the docs give the impression that by overriding Storage.path() you can mangle file names however you'd like, but that doesn't seem to have any effect on the FileField of my model. It would help if there was a diagram or even several sentences that talked about how these classes fit together to produce Django's file management (ex. which class is authoritative for what information and what time in the file upload/save/retrieval process).
There's two other problems besides the mechanics of file system management. One is that the model that contains the file is itself a member of another model, making form management more laborious than it could be (I think). Second, I need to import a few thousand files via an import script and it doesn't seem quite right that I would just manually set the FileField's value (not a serious problem, it's just import, but it seems to indicate a deeper misstep). Surely there's a simpler way of having database-backed file metadata than what I've hacked here.
I think what would be ideal is if I could subclass models.Model and files.File together, but that seems inadvisable. Are there any simple examples of people doing the kind of thing I'm doing? Or is there somewhere in the docs or mailing list that I missed that has the one or two sentences that I need to read that will clear it all up? It doesn't seem complex, but it's turning out that way.
Bart
-- You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/xmVUt1NBmfkJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment