ObjectDownloadView¶
ObjectDownloadView
serves files managed in models with file fields
such as FileField
or
ImageField
.
Use this view like Django’s builtin
DetailView
.
Additional options allow you to store file metadata (size, content-type, ...) in the model, as deserialized fields.
Simple example¶
Given a model with a FileField
:
from django.db import models
class Document(models.Model):
slug = models.SlugField()
file = models.FileField(upload_to='object')
Setup a view to stream the file
attribute:
from django_downloadview import ObjectDownloadView
from demoproject.object.models import Document
default_file_view = ObjectDownloadView.as_view(model=Document)
ObjectDownloadView
inherits from
BaseDetailView
, i.e. it expects either
slug
or pk
:
from django.conf.urls import patterns, url
from demoproject.object import views
urlpatterns = patterns(
'',
url(r'^default-file/(?P<slug>[a-zA-Z0-9_-]+)/$',
views.default_file_view,
name='default_file'),
)
Base options¶
ObjectDownloadView
inherits from
DownloadMixin
, which has various
options such as basename
or attachment
.
Serving specific file field¶
If your model holds several file fields, or if the file field name is not
“file”, you can use ObjectDownloadView.file_field
to specify the field
to use.
Here is a model where there are two file fields:
from django.db import models
class Document(models.Model):
slug = models.SlugField()
file = models.FileField(upload_to='object')
another_file = models.FileField(upload_to='object-other')
Then here is the code to serve “another_file” instead of the default “file”:
from django_downloadview import ObjectDownloadView
from demoproject.object.models import Document
another_file_view = ObjectDownloadView.as_view(
model=Document,
file_field='another_file')
Mapping file attributes to model’s¶
Sometimes, you use Django model to store file’s metadata. Some of this metadata can be used when you serve the file.
As an example, let’s consider the client-side basename lives in model and not in storage:
from django.db import models
class Document(models.Model):
slug = models.SlugField()
file = models.FileField(upload_to='object')
basename = models.CharField(max_length=100)
Then you can configure the ObjectDownloadView.basename_field
option:
from django_downloadview import ObjectDownloadView
from demoproject.object.models import Document
deserialized_basename_view = ObjectDownloadView.as_view(
model=Document,
basename_field='basename')
Note
basename
could have been a model’s property instead of a CharField
.
See details below for a full list of options.
API reference¶
-
class
django_downloadview.views.object.
ObjectDownloadView
(**kwargs)¶ Bases:
django.views.generic.detail.SingleObjectMixin
,django_downloadview.views.base.BaseDownloadView
Serve file fields from models.
This class extends
SingleObjectMixin
, so you can use its arguments to target the instance to operate on:slug
,slug_kwarg
,model
,queryset
...In addition to
SingleObjectMixin
arguments, you can set arguments related to the file to be downloaded:file_field
;basename_field
;encoding_field
;mime_type_field
;charset_field
;modification_time_field
;size_field
.
file_field
is the main one. Other arguments are provided for convenience, in case your model holds some (deserialized) metadata about the file, such as its basename, its modification time, its MIME type... These fields may be particularly handy if your file storage is not the local filesystem.-
file_field
= 'file'¶ Name of the model’s attribute which contains the file to be streamed. Typically the name of a FileField.
-
basename_field
= None¶ Optional name of the model’s attribute which contains the basename.
-
encoding_field
= None¶ Optional name of the model’s attribute which contains the encoding.
-
mime_type_field
= None¶ Optional name of the model’s attribute which contains the MIME type.
-
charset_field
= None¶ Optional name of the model’s attribute which contains the charset.
-
modification_time_field
= None¶ Optional name of the model’s attribute which contains the modification
-
size_field
= None¶ Optional name of the model’s attribute which contains the size.
-
get_file
()¶ Return
FieldFile
instance.The file wrapper is model’s field specified as
file_field
. It is typically aFieldFile
or subclass.Raises
FileNotFound
if instance’s field is empty.Additional attributes are set on the file wrapper if
encoding
,mime_type
,charset
,modification_time
orsize
are configured.
-
get_basename
()¶ Return client-side filename.
-
get
(request, *args, **kwargs)¶