Monday, May 31, 2010

Problems with Django File Fields

Hi All,

The standard Django File Fields (both on models and the form
equivalent) have a number of shortcomings from my point of view / in
my field. The most important of these are:

* The result of uploaded file fields are assumed to be "public", i.e.
served through the MEDIA url. I would rather store the files somewhere
safe and determine in my view who gets to download what.
* The filename is stored in the database, including the (potentially
long) path to the file. This in itself creates all kinds of trouble:
* What if I want to move a file?
* What if the full path is longer than 255 characters?
* What if I create (using objects) a separate directory structure?
Why is there now a global uniqueness constraint on variable names?

As far as I see the custom storage system does not fix any of the
above problems, since it is "name" based.

For a number of my applications I've worked around the above problems
by simply coding around it: I'm storing the filename in the DB in a
CharField, and manually read/write the file from/to disk in view
operations. I'm using the [appname]/[modelname]/[fieldname]/id as a
path.

Obviously I'd like to factor out the above. So I tried
http://docs.djangoproject.com/en/1.2/howto/custom-model-fields/

I'm running into a bunch of problems:
* The 'real' django FileField does not follow the pattern from the
article above (i.e. search for to_python in the source). What's going
on here?
* As described above, I'd like to use the pk of an object to write the
file. However, the pk is not available before the save is done (at the
model level). Are there known solutions for this problem?

What paths could you recommend for me? Are my observations on the
limitations of FileFields correct? Have some of you tried and found
(different?) solutions?

thanks a bunch.

regards,
Klaas van Schelven

--
You received this message because you are subscribed to the Google Groups "Django users" group.
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