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'),
)

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 BaseDetailView, so you can use its arguments to target the instance to operate on: slug, slug_kwarg, model, queryset... See Django’s DetailView reference for details.

In addition to BaseDetailView arguments, you can set arguments related to the file to be downloaded.

The main one is file_field.

The 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 a FieldFile or subclass.

Additional attributes are set on the file wrapper if encoding, mime_type, charset, modification_time or size are configured.

get_basename()

Return client-side filename.

get(request, *args, **kwargs)
Read the Docs v: 1.3
Versions
latest
1.3
1.2
1.1
1.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.