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.
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'),
)
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')
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.
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 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.
Name of the model’s attribute which contains the file to be streamed. Typically the name of a FileField.
Optional name of the model’s attribute which contains the basename.
Optional name of the model’s attribute which contains the encoding.
Optional name of the model’s attribute which contains the MIME type.
Optional name of the model’s attribute which contains the charset.
Optional name of the model’s attribute which contains the modification
Optional name of the model’s attribute which contains the size.
Return FieldFile instance.
The file wrapper is model’s field specified as file_field. It is typically a FieldFile 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 or size are configured.
Return client-side filename.