File: extra.rst

package info (click to toggle)
django-select2 7.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 452 kB
  • sloc: python: 1,654; javascript: 66; makefile: 3
file content (122 lines) | stat: -rw-r--r-- 3,515 bytes parent folder | download
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
Extra
=====

Chained select2
---------------

Suppose you have an address form where a user should choose a Country and a City.
When the user selects a country we want to show only cities belonging to that country.
So the one selector depends on another one.

Models
``````

Here are our two models:

.. code-block:: python

    class Country(models.Model):
        name = models.CharField(max_length=255)


    class City(models.Model):
        name = models.CharField(max_length=255)
        country = models.ForeignKey('Country', related_name="cities")


Customizing a Form
``````````````````

Lets link two widgets via a *dependent_fields* dictionary. The key represents the name of 
the field in the form. The value represents the name of the field in the model (used in `queryset`).

.. code-block:: python
    :emphasize-lines: 17

    class AddressForm(forms.Form):
        country = forms.ModelChoiceField(
            queryset=Country.objects.all(),
            label=u"Country",
            widget=ModelSelect2Widget(
                model=Country,
                search_fields=['name__icontains'],
            )
        )

        city = forms.ModelChoiceField(
            queryset=City.objects.all(),
            label=u"City",
            widget=ModelSelect2Widget(
                model=City,
                search_fields=['name__icontains'],
                dependent_fields={'country': 'country'},
                max_results=500,
            )
        )


Interdependent select2
----------------------

Also you may want not to restrict the user to which field should be selected first.
Instead you want to suggest to the user options for any select2 depending of his selection in another one.

Customize the form in a manner:

.. code-block:: python
    :emphasize-lines: 7

    class AddressForm(forms.Form):
        country = forms.ModelChoiceField(
            queryset=Country.objects.all(),
            label=u"Country",
            widget=ModelSelect2Widget(
                search_fields=['name__icontains'],
                dependent_fields={'city': 'cities'},
            )
        )

        city = forms.ModelChoiceField(
            queryset=City.objects.all(),
            label=u"City",
            widget=ModelSelect2Widget(
                search_fields=['name__icontains'],
                dependent_fields={'country': 'country'},
                max_results=500,
            )
        )

Take attention to country's dependent_fields. The value of 'city' is 'cities' because of
related name used in a filter condition `cities` which differs from widget field name `city`.

.. caution::
    Be aware of using interdependent select2 in parent-child relation.
    When a child is selected, you are restricted to change parent (only one value is available).
    Probably you should let the user reset the child first to release parent select2.


Multi-dependent select2
-----------------------

Furthermore you may want to filter options on two or more select2 selections (some code is dropped for clarity):

.. code-block:: python
    :emphasize-lines: 14

    class SomeForm(forms.Form):
        field1 = forms.ModelChoiceField(
            widget=ModelSelect2Widget(
            )
        )

        field2 = forms.ModelChoiceField(
            widget=ModelSelect2Widget(
            )
        )

        field3 = forms.ModelChoiceField(
            widget=ModelSelect2Widget(
                dependent_fields={'field1': 'field1', 'field2': 'field2'},
            )
        )