The FileBrowseField is a custom model field which returns a FileObject. The widgets FileInput and ClearableFileInput are used with the admin app in order to show an additional thumbnail for images.
A subclass of CharField, referencing a media file within. Returns a FileObject.
Parameters: |
|
---|
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)
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 %}
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"
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']
Subclass of FileInput with an additional thumbnail:
from filebrowser.widgets import FileInput
class BlogEntryOptions(admin.ModelAdmin):
formfield_overrides = {
models.ImageField: {'widget': FileInput},
}
Subclass of ClearableFileInput with an additional thumbnail:
from filebrowser.widgets import ClearableFileInput
class BlogEntryOptions(admin.ModelAdmin):
formfield_overrides = {
models.ImageField: {'widget': ClearableFileInput},
}
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.