Fields & Widgets

The FileBrowseField is a custom model field which returns a FileObject.

FileBrowseField

class FileBrowseField(max_length[, site, directory, extensions, format, **options])

A subclass of CharField, referencing a media file within. Returns a FileObject.

Parameters:
  • site – A FileBrowser site (defaults to the main site), see FileBrowser Site.
  • directory – Directory to browse when clicking the search icon.
  • extensions – List of allowed extensions, see Extensions and Formats.
  • format – A key from SELECT_FORMATS in order to restrict the selection to specific filetypes, see Extensions and Formats.

For example:

from filebrowser.fields import FileBrowseField

class BlogEntry(models.Model):
    image = FileBrowseField("Image", max_length=200, directory="images/", extensions=[".jpg"], blank=True, null=True)
    document = FileBrowseField("PDF", max_length=200, directory="documents/", extensions=[".pdf",".doc"], blank=True, null=True)

FileBrowseField in Templates

You can use all attributes from FileObject:

{{ blogentry.image }}
<img src="{{ publication.image.url }}" />

{% ifequal blogentry.image.image_orientation "landscape" %}
    <img src="{{ blogentry.image.url }}" class="landscape" />
{% endifequal %}

Showing Thumbnail in the Changelist

To show a thumbnail with the changelist, you can define a ModelAdmin method:

from filebrowser.settings import ADMIN_THUMBNAIL

def image_thumbnail(self, obj):
    if obj.image and obj.image.filetype == "Image":
        return '<img src="%s" />' % obj.image.version_generate(ADMIN_THUMBNAIL).url
    else:
        return ""
image_thumbnail.allow_tags = True
image_thumbnail.short_description = "Thumbnail"

Using the FileBrowseField with TinyMCE

In order to replace the TinyMCE image/file manager with the FileBrowser, you have to use a FileBrowser Callback. There’s an example TinyMCE configuration file in /static/js/ called TinyMCEAdmin.js. You can either copy the FileBrowserCallback to your own file or just use tinymce_setup.js (which comes with django-grappelli).

Just add these lines to your ModelAdmin asset definitions:

class Media:
    js = ['/path/to/tinymce/jscripts/tiny_mce/tiny_mce.js',
          '/path/to/your/tinymce_setup.js']

Django FileField and the FileBrowser

Return a FileObject from a FileField or ImageField with:

from filebrowser.base import FileObject

image_upload = models.ImageField(u"Image (Upload)", max_length=250, upload_to=image_upload_path, blank=True, null=True)

def image(self):
    if self.image_upload:
        return FileObject(self.image_upload.path)
    return None

In order show a thumbnail with your changelist, you could use a ModelAdmin method:

from filebrowser.base import FileObject

def image_thumbnail(self, obj):
    if obj.image_upload:
        image = FileObject(obj.image_upload.path)
        if image.filetype == "Image":
            return '<img src="%s" />' % image.version_generate(ADMIN_THUMBNAIL).url
    else:
        return ""
image_thumbnail.allow_tags = True
image_thumbnail.short_description = "Thumbnail"

Note

There are different ways to achieve this. The above examples show one of several options.