| 1 |
Extra |
| 2 |
===== |
| 3 |
|
| 4 |
Chained select2 |
| 5 |
--------------- |
| 6 |
|
| 7 |
Suppose you have an address form where a user should choose a Country and a City. |
| 8 |
When the user selects a country we want to show only cities belonging to that country. |
| 9 |
So the one selector depends on another one. |
| 10 |
|
| 11 |
Models |
| 12 |
`````` |
| 13 |
|
| 14 |
Here are our two models: |
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
|
| 18 |
class Country(models.Model): |
| 19 |
name = models.CharField(max_length=255) |
| 20 |
|
| 21 |
|
| 22 |
class City(models.Model): |
| 23 |
name = models.CharField(max_length=255) |
| 24 |
country = models.ForeignKey('Country', related_name="cities") |
| 25 |
|
| 26 |
|
| 27 |
Customizing a Form |
| 28 |
`````````````````` |
| 29 |
|
| 30 |
Lets link two widgets via a *dependent_fields* dictionary. The key represents the name of |
| 31 |
the field in the form. The value represents the name of the field in the model (used in `queryset`). |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
:emphasize-lines: 17 |
| 35 |
|
| 36 |
class AddressForm(forms.Form): |
| 37 |
country = forms.ModelChoiceField( |
| 38 |
queryset=Country.objects.all(), |
| 39 |
label=u"Country", |
| 40 |
widget=ModelSelect2Widget( |
| 41 |
model=Country, |
| 42 |
search_fields=['name__icontains'], |
| 43 |
) |
| 44 |
) |
| 45 |
|
| 46 |
city = forms.ModelChoiceField( |
| 47 |
queryset=City.objects.all(), |
| 48 |
label=u"City", |
| 49 |
widget=ModelSelect2Widget( |
| 50 |
model=City, |
| 51 |
search_fields=['name__icontains'], |
| 52 |
dependent_fields={'country': 'country'}, |
| 53 |
max_results=500, |
| 54 |
) |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
Interdependent select2 |
| 59 |
---------------------- |
| 60 |
|
| 61 |
Also you may want not to restrict the user to which field should be selected first. |
| 62 |
Instead you want to suggest to the user options for any select2 depending of his selection in another one. |
| 63 |
|
| 64 |
Customize the form in a manner: |
| 65 |
|
| 66 |
.. code-block:: python |
| 67 |
:emphasize-lines: 7 |
| 68 |
|
| 69 |
class AddressForm(forms.Form): |
| 70 |
country = forms.ModelChoiceField( |
| 71 |
queryset=Country.objects.all(), |
| 72 |
label=u"Country", |
| 73 |
widget=ModelSelect2Widget( |
| 74 |
search_fields=['name__icontains'], |
| 75 |
dependent_fields={'city': 'cities'}, |
| 76 |
) |
| 77 |
) |
| 78 |
|
| 79 |
city = forms.ModelChoiceField( |
| 80 |
queryset=City.objects.all(), |
| 81 |
label=u"City", |
| 82 |
widget=ModelSelect2Widget( |
| 83 |
search_fields=['name__icontains'], |
| 84 |
dependent_fields={'country': 'country'}, |
| 85 |
max_results=500, |
| 86 |
) |
| 87 |
) |
| 88 |
|
| 89 |
Take attention to country's dependent_fields. The value of 'city' is 'cities' because of |
| 90 |
related name used in a filter condition `cities` which differs from widget field name `city`. |
| 91 |
|
| 92 |
.. caution:: |
| 93 |
Be aware of using interdependent select2 in parent-child relation. |
| 94 |
When a child is selected, you are restricted to change parent (only one value is available). |
| 95 |
Probably you should let the user reset the child first to release parent select2. |
| 96 |
|
| 97 |
|
| 98 |
Multi-dependent select2 |
| 99 |
----------------------- |
| 100 |
|
| 101 |
Furthermore you may want to filter options on two or more select2 selections (some code is dropped for clarity): |
| 102 |
|
| 103 |
.. code-block:: python |
| 104 |
:emphasize-lines: 14 |
| 105 |
|
| 106 |
class SomeForm(forms.Form): |
| 107 |
field1 = forms.ModelChoiceField( |
| 108 |
widget=ModelSelect2Widget( |
| 109 |
) |
| 110 |
) |
| 111 |
|
| 112 |
field2 = forms.ModelChoiceField( |
| 113 |
widget=ModelSelect2Widget( |
| 114 |
) |
| 115 |
) |
| 116 |
|
| 117 |
field3 = forms.ModelChoiceField( |
| 118 |
widget=ModelSelect2Widget( |
| 119 |
dependent_fields={'field1': 'field1', 'field2': 'field2'}, |
| 120 |
) |
| 121 |
) |
| 122 |
|