File: formset-views.rst

package info (click to toggle)
python-django-extra-views 0.14.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 468 kB
  • sloc: python: 1,591; makefile: 142; sh: 6
file content (282 lines) | stat: -rw-r--r-- 9,067 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
Formset Views
=============

For all of these views we've tried to mimic the API of Django's existing class-based
views as closely as possible, so they should feel natural to anyone who's already
familiar with Django's views.


FormSetView
-----------

This is the formset equivalent of Django's FormView. Use it when you want to
display a single (non-model) formset on a page.

A simple formset:

.. code-block:: python

    from extra_views import FormSetView
    from my_app.forms import AddressForm
    

    class AddressFormSetView(FormSetView):
        template_name = 'address_formset.html'
        form_class = AddressForm
        success_url = 'success/'

        def get_initial(self):
            # return whatever you'd normally use as the initial data for your formset.
          return data

        def formset_valid(self, formset):
            # do whatever you'd like to do with the valid formset
            return super(AddressFormSetView, self).formset_valid(formset)

and in ``address_formset.html``:

.. code-block:: html

    <form method="post">
      ...
      {{ formset }}
      ...
      <input type="submit" value="Submit" />
    </form>

This view will render the template ``address_formset.html`` with a context variable
:code:`formset` representing the :code:`AddressFormSet`. Once POSTed and successfully
validated, :code:`formset_valid` will be called (which is where your handling logic
goes), then the view will redirect to :code:`success_url`.

Formset constructor and factory kwargs
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

FormSetView exposes all the parameters you'd normally be able to pass to the
:code:`django.forms.BaseFormSet` constructor and
:code:`django.forms.formset_factory()`. This can be done by setting the
respective attribute on the class, or :code:`formset_kwargs` and
:code:`factory_kwargs` at the class level.

Below is an exhaustive list of all formset-related attributes which can be set
at the class level for :code:`FormSetView`:

.. code-block:: python

    ...
    from my_app.forms import AddressForm, BaseAddressFormSet


    class AddressFormSetView(FormSetView):
        template_name = 'address_formset.html'
        form_class = AddressForm
        formset_class = BaseAddressFormSet
        initial = [{'type': 'home'}, {'type': 'work'}]
        prefix = 'address-form'
        success_url = 'success/'
        factory_kwargs = {'extra': 2, 'max_num': None,
                          'can_order': False, 'can_delete': False}
        formset_kwargs = {'auto_id': 'my_id_%s'}

In the above example, BaseAddressFormSet would be a subclass of
:code:`django.forms.BaseFormSet`.

ModelFormSetView
----------------

ModelFormSetView makes use of :code:`django.forms.modelformset_factory()`, using the
declarative syntax used in :code:`FormSetView` as well as Django's own class-based
views. So as you'd expect, the simplest usage is as follows:

.. code-block:: python

    from extra_views import ModelFormSetView
    from my_app.models import Item


    class ItemFormSetView(ModelFormSetView):
        model = Item
        fields = ['name', 'sku', 'price']
        template_name = 'item_formset.html'

Rather than setting :code:`fields`, :code:`exclude` can be defined
at the class level as a list of fields to be excluded.

It is not necessary to define :code:`fields` or :code:`exclude` if a
:code:`form_class` is defined at the class level:

.. code-block:: python

    ...
    from django.forms import ModelForm


    class ItemForm(ModelForm):
        # Custom form definition goes here
        fields = ['name', 'sku', 'price']


    class ItemFormSetView(ModelFormSetView):
        model = Item
        form_class = ItemForm
        template_name = 'item_formset.html'

Like :code:`FormSetView`, the :code:`formset` variable is made available in the template
context. By default this will populate the formset with all the instances of
:code:`Item` in the database. You can control this by overriding :code:`get_queryset` on
the class, which could filter on a URL kwarg (:code:`self.kwargs`), for example:

.. code-block:: python

    class ItemFormSetView(ModelFormSetView):
        model = Item
        template_name = 'item_formset.html'

        def get_queryset(self):
            sku = self.kwargs['sku']
            return super(ItemFormSetView, self).get_queryset().filter(sku=sku)


InlineFormSetView
-----------------

When you want to edit instances of a particular model related to a parent model
(using a ForeignKey), you'll want to use InlineFormSetView. An example use case
would be editing addresses associated with a particular contact.

.. code-block:: python

    from extra_views import InlineFormSetView


    class EditContactAddresses(InlineFormSetView):
        model = Contact
        inline_model = Address

        ...

Aside from the use of :code:`model` and :code:`inline_model`,
:code:`InlineFormSetView` works more-or-less in the same way as
:code:`ModelFormSetView`, instead calling :code:`django.forms.inlineformset_factory()`.

CreateWithInlinesView and UpdateWithInlinesView
-----------------------------------------------

These are the most powerful views in the library, they are effectively
replacements for Django's own :code:`CreateView` and :code:`UpdateView`. The key
difference is that they let you include any number of inline formsets (as well
as the parent model's form). This provides functionality much like the Django
Admin change forms. The API should be fairly familiar as well. The list of the
inlines will be passed to the template as context variable `inlines`.

Here is a simple example that demonstrates the use of each view with normal
inline relationships:

.. code-block:: python

    from extra_views import CreateWithInlinesView, UpdateWithInlinesView, InlineFormSetFactory


    class ItemInline(InlineFormSetFactory):
        model = Item
        fields = ['sku', 'price', 'name']


    class ContactInline(InlineFormSetFactory):
        model = Contact
        fields = ['name', 'email']


    class CreateOrderView(CreateWithInlinesView):
        model = Order
        inlines = [ItemInline, ContactInline]
        fields = ['customer', 'name']
        template_name = 'order_and_items.html'

        def get_success_url(self):
            return self.object.get_absolute_url()


    class UpdateOrderView(UpdateWithInlinesView):
        model = Order
        inlines = [ItemInline, ContactInline]
        fields = ['customer', 'name']
        template_name = 'order_and_items.html'

        def get_success_url(self):
            return self.object.get_absolute_url()

and in the html template:

.. code-block:: html

    <form method="post">
      ...
      {{ form }}

      {% for formset in inlines %}
        {{ formset }}
      {% endfor %}
      ...
      <input type="submit" value="Submit" />
    </form>

InlineFormSetFactory
^^^^^^^^^^^^^^^^^^^^
This class represents all the configuration necessary to generate an inline formset
from :code:`django.inlineformset_factory()`. Each class within in
:code:`CreateWithInlines.inlines` and :code:`UpdateWithInlines.inlines`
should be a subclass of :code:`InlineFormSetFactory`. All the
same methods and attributes as :code:`InlineFormSetView` are available, with the
exception of any view-related attributes and methods, such as :code:`success_url`
or :code:`formset_valid()`:

.. code-block:: python

    from my_app.forms import ItemForm, BaseItemFormSet
    from extra_views import InlineFormSetFactory


    class ItemInline(InlineFormSetFactory):
        model = Item
        form_class = ItemForm
        formset_class = BaseItemFormSet
        initial = [{'name': 'example1'}, {'name', 'example2'}]
        prefix = 'item-form'
        factory_kwargs = {'extra': 2, 'max_num': None,
                          'can_order': False, 'can_delete': False}
        formset_kwargs = {'auto_id': 'my_id_%s'}


**IMPORTANT**: Note that when using :code:`InlineFormSetFactory`, :code:`model` should be the
*inline* model and **not** the parent model.

GenericInlineFormSetView
------------------------

In the specific case when you would usually use Django's
:code:`django.contrib.contenttypes.forms.generic_inlineformset_factory()`, you
should use :code:`GenericInlineFormSetView`. The kwargs :code:`ct_field` and
:code:`fk_field` should be set in :code:`factory_kwargs` if they need to be
changed from their default values:

.. code-block:: python

    from extra_views.generic import GenericInlineFormSetView


    class EditOrderTags(GenericInlineFormSetView):
        model = Order
        inline_model = Tag
        factory_kwargs = {'ct_field': 'content_type', 'fk_field': 'object_id',
                          'max_num': 1}
        formset_kwargs = {'save_as_new': True}

        ...

There is a :code:`GenericInlineFormSetFactory` which is analogous to
:code:`InlineFormSetFactory` for use with generic inline formsets.

:code:`GenericInlineFormSetFactory` can be used in
:code:`CreateWithInlines.inlines` and :code:`UpdateWithInlines.inlines` in the
obvious way.