views:

884

answers:

2

I'm looking for a robust, stable django file manager for use in the django admin.

My requirements wish list:

  1. Allows browsing and selecting files on the server (e.g. images)
  2. Allows uploading files. Multiple file upload would be great (with, e.g. uploadify)
  3. Does not require me to use a custom field in my model definitions (like django-filebrowser does). I want something that can ideally be attached to a CharField (or ImageField of FileField) in admin.py, like Carl Meyer's django-adminfiles.

I've used django-filebrowser (non-grappelli version) and also looked at (but not used) django-adminfiles. Both are very nice. But django-filebrowser requires using a custom field in my Models, plus I don't want the 'versions' (multiple image sizes) functionality. django-adminfiles is for inserting files as inlines into textareas, so is not what I'm looking for.

I'm happy to modify one of these to suit my needs, but would hate to do so if there are some other alternatives out there I'm missing.

+1  A: 

FWIW, django-adminfiles also has buried in it some fledgling functionality to use the file-browser as a select-dropdown replacement: so your model would have a ForeignKey to the "FileUpload" model, and you'd get to browse to fill that ForeignKey. Is that closer to what you're looking for?

I haven't needed or used that feature in quite some time, it's not documented or tested, and there's been a lot of rewriting since I added it, so it may or may not be in working condition. But it's on my todo list to get it back in working condition one of these days, and I certainly wouldn't object to a little motivated help ;-)

Carl Meyer
That's closer to what I'm looking for, but not quite. In the interest of eliminating dependencies on other apps, I'd like my Model to be able to get by with a CharField/FileField/ImageField. Then the file manager would just update the CharField with the relative path based on the user selection. Looks like django-adminfiles is doing something similar for the "inlines" insertion into textareas. If no other answers pop up here, I'll probably try changing your django-adminfiles. (Nice app by the way. Thanks!)
zlovelady
If there's a widget for that, it might be possible to modify the widget and use it as a widget on a `FilePathField`, which I think is like a `CharField` that limits choices to a file in a particular path.
LeafStorm
@zlovelady - that's a really good idea for decoupled use; I wouldn't be averse to pulling such a feature into django-adminfiles.
Carl Meyer
@Carl Meyer - great. If I get around to implementing that, I'll make of fork of django-adminfiles on github.
zlovelady
+1  A: 

Since posting the question, I found that django-filebrowser has a FileBrowseWidget. With a few ugly hacks applied, I was able to get it working in the django admin on ImageField and FileField (no more filebrowser.fields.FileBrowseField needed on the model).

I'm using this like so (with the non-grappelli-dependent django-filebrowser from wardi at github), and it seems to be working.

# a few changes to filebrowser/fields.py

class FileBrowseWidget(Input):
    ...

    # change the default value of attrs from None to {}
    def __init__(self, attrs={}):
       ... # the rest unchanged

    # change the default value of attrs, and the first few lines of render, like so
    def render(self, name, value, attrs={}):
        if value is None:
            value = ""
        else:
            # set an attribute on value that the filebrowser templates need in 
            # order to display the thumbnail photo in the admin
            for suffix in ['gif', 'GIF', 'jpg', 'JPG', 'png', 'PNG']:
                if hasattr(value, 'path') and value.path.endswith("." + suffix):
                    value.filetype = "Image"
                    break
        ... # the rest unchanged


# admin.py

from filebrowser.fields import FileBrowseWidget

class FileBrowseForm(forms.ModelForm):
    # Use a CharField, not an ImageField or FileField, since filebrowser
    # is handling any file uploading
    image = forms.CharField(required=True, widget=FileBrowseWidget())

class SomeModelAdmin(admin.ModelAdmin):
    # SomeModel has an ImageField named image
    form = FileBrowseForm

    ... # the rest of the admin definition

This is a bit ugly, but seems to be working for now. It eliminates the Model-level dependency on django-filebrowser and pushes the dependency to the admin, which is where I wanted it.

zlovelady