Debian Codegraph @

docs/index.rst

raw
1 .. include:: ../README.rst
2
3 Installation
4 ------------
5
6 Install ``django-select2``::
7
8 python3 -m pip install django-select2
9
10 Add ``django_select2`` to your ``INSTALLED_APPS`` in your project settings.
11
12 Add ``django_select`` to your URL root configuration:
13
14 .. code-block:: python
15
16 from django.urls import include, path
17
18 urlpatterns = [
19 # … other patterns
20 path("select2/", include("django_select2.urls")),
21 # … other patterns
22 ]
23
24
25 ``django-select2`` requires a cache backend which is **persistent**
26 across all application servers..
27
28 **This means that the** :class:`.DummyCache` **backend will not work!**
29
30 The default cache backend is :class:`.LocMemCache`, which is persistent
31 across a single node. For projects with a single application server
32 this will work fine, however you will run into issues when
33 you scale up into multiple servers.
34
35 Below is an example setup using Redis, which is a solution that
36 works for multi-server setups:
37
38 Make sure you have a Redis server up and running::
39
40 # Debian
41 sudo apt-get install redis-server
42
43 # macOS
44 brew install redis
45
46 # install Redis python client
47 python3 -m pip install django-redis
48
49 Next, add the cache configuration to your ``settings.py`` as follows:
50
51 .. code-block:: python
52
53 CACHES = {
54 # … default cache config and others
55 "select2": {
56 "BACKEND": "django_redis.cache.RedisCache",
57 "LOCATION": "redis://127.0.0.1:6379/2",
58 "OPTIONS": {
59 "CLIENT_CLASS": "django_redis.client.DefaultClient",
60 }
61 }
62 }
63
64 # Tell select2 which cache configuration to use:
65 SELECT2_CACHE_BACKEND = "select2"
66
67 .. note::
68 A custom timeout for your cache backend, will serve as an indirect session limit.
69 Auto select fields will stop working after, once the cache has expired.
70 It's recommended to use a dedicated cache database with an adequate
71 cache replacement policy such as LRU, FILO, etc.
72
73
74 External Dependencies
75 ---------------------
76
77 - jQuery is not included in the package since it is
78 expected that in most scenarios this would already be available.
79
80
81 Quick Start
82 -----------
83
84 Here is a quick example to get you started:
85
86 First make sure you followed the installation instructions above.
87 Once everything is setup, let's start with a simple example.
88
89 We have the following model:
90
91 .. code-block:: python
92
93 # models.py
94 from django.conf import settings
95 from django.db import models
96
97
98 class Book(models.Model):
99 author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
100 co_authors = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name='co_authored_by')
101
102
103 Next, we create a model form with custom Select2 widgets.
104
105 .. code-block:: python
106
107 # forms.py
108 from django import forms
109 from django_select2 import forms as s2forms
110
111 from . import models
112
113
114 class AuthorWidget(s2forms.ModelSelect2Widget):
115 search_fields = [
116 "username__icontains",
117 "email__icontains",
118 ]
119
120
121 class CoAuthorsWidget(s2forms.ModelSelect2MultipleWidget):
122 search_fields = [
123 "username__icontains",
124 "email__icontains",
125 ]
126
127
128 class BookForm(forms.ModelForm):
129 class Meta:
130 model = models.Book
131 fields = "__all__"
132 widgets = {
133 "author": AuthorWidget,
134 "co_authors": CoAuthorsWidget,
135 }
136
137 A simple class based view will do, to render your form:
138
139 .. code-block:: python
140
141 # views.py
142 from django.views import generic
143
144 from . import forms, models
145
146
147 class BookCreateView(generic.CreateView):
148 model = models.Book
149 form_class = forms.BookForm
150 success_url = "/"
151
152 Make sure to add the view to your ``urls.py``:
153
154 .. code-block:: python
155
156 # urls.py
157 from django.urls import include, path
158
159 from . import views
160
161 urlpatterns = [
162 # … other patterns
163 path("select2/", include("django_select2.urls")),
164 # … other patterns
165 path("book/create", views.BookCreateView.as_view(), name="book-create"),
166 ]
167
168
169 Finally, we need a little template, ``myapp/templates/myapp/book_form.html``
170
171 .. code-block:: HTML
172
173 <!DOCTYPE html>
174 <html lang="en">
175 <head>
176 <title>Create Book</title>
177 {{ form.media.css }}
178 <style>
179 input, select {width: 100%}
180 </style>
181 </head>
182 <body>
183 <h1>Create a new Book</h1>
184 <form method="POST">
185 {% csrf_token %}
186 {{ form.as_p }}
187 <input type="submit">
188 </form>
189 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
190 {{ form.media.js }}
191 </body>
192 </html>
193
194 Done - enjoy the wonders of Select2!
195
196
197 Changelog
198 ---------
199
200 See `Github releases`_.
201
202 .. _Github releases: https://github.com/codingjoe/django-select2/releases
203
204 All Contents
205 ============
206
207 Contents:
208
209 .. toctree::
210 :maxdepth: 2
211 :glob:
212
213 django_select2
214 extra
215 CONTRIBUTING
216
217 Indices and tables
218 ==================
219
220 * :ref:`genindex`
221 * :ref:`modindex`
222 * :ref:`search`