Debian Codegraph @

docs/views/object.txt

raw
1 ##################
2 ObjectDownloadView
3 ##################
4
5 .. py:module:: django_downloadview.views.object
6
7 :class:`ObjectDownloadView` **serves files managed in models with file fields**
8 such as :class:`~django.db.models.FileField` or
9 :class:`~django.db.models.ImageField`.
10
11 Use this view like Django's builtin
12 :class:`~django.views.generic.detail.DetailView`.
13
14 Additional options allow you to store file metadata (size, content-type, ...)
15 in the model, as deserialized fields.
16
17
18 **************
19 Simple example
20 **************
21
22 Given a model with a :class:`~django.db.models.FileField`:
23
24 .. literalinclude:: /../demo/demoproject/object/models.py
25 :language: python
26 :lines: 1-6
27
28
29 Setup a view to stream the ``file`` attribute:
30
31 .. literalinclude:: /../demo/demoproject/object/views.py
32 :language: python
33 :lines: 1-6
34
35 :class:`~django_downloadview.views.object.ObjectDownloadView` inherits from
36 :class:`~django.views.generic.detail.BaseDetailView`, i.e. it expects either
37 ``slug`` or ``pk``:
38
39 .. literalinclude:: /../demo/demoproject/object/urls.py
40 :language: python
41 :lines: 1-7, 8-11, 27
42
43
44 ************
45 Base options
46 ************
47
48 :class:`ObjectDownloadView` inherits from
49 :class:`~django_downloadview.views.base.DownloadMixin`, which has various
50 options such as :attr:`~django_downloadview.views.base.DownloadMixin.basename`
51 or :attr:`~django_downloadview.views.base.DownloadMixin.attachment`.
52
53
54 ***************************
55 Serving specific file field
56 ***************************
57
58 If your model holds several file fields, or if the file field name is not
59 "file", you can use :attr:`ObjectDownloadView.file_field` to specify the field
60 to use.
61
62 Here is a model where there are two file fields:
63
64 .. literalinclude:: /../demo/demoproject/object/models.py
65 :language: python
66 :lines: 1-6, 7
67
68 Then here is the code to serve "another_file" instead of the default "file":
69
70 .. literalinclude:: /../demo/demoproject/object/views.py
71 :language: python
72 :lines: 1-4, 8-11
73
74
75 **********************************
76 Mapping file attributes to model's
77 **********************************
78
79 Sometimes, you use Django model to store file's metadata. Some of this metadata
80 can be used when you serve the file.
81
82 As an example, let's consider the client-side basename lives in model and not
83 in storage:
84
85 .. literalinclude:: /../demo/demoproject/object/models.py
86 :language: python
87 :lines: 1-6, 8
88
89 Then you can configure the :attr:`ObjectDownloadView.basename_field` option:
90
91 .. literalinclude:: /../demo/demoproject/object/views.py
92 :language: python
93 :lines: 1-4, 13-17
94
95 .. note::
96
97 ``basename`` could have been a model's property instead of a ``CharField``.
98
99 See details below for a full list of options.
100
101
102 *************
103 API reference
104 *************
105
106 .. autoclass:: ObjectDownloadView
107 :members:
108 :undoc-members:
109 :show-inheritance:
110 :member-order: bysource