@@ -22,6 +22,8 @@ through the model's ``Meta`` class. Here's an example ``models.py``:
2222
2323.. code :: python
2424
25+ from django.db import models
26+
2527 class SortableBook (models .Model ):
2628 title = models.CharField(
2729 " Title" ,
@@ -141,6 +143,35 @@ we simple use the mixin class :class:`adminsortable2.admin.SortableAdminMixin`.
141143
142144Example:
143145
146+ A typical use case is a model ``Book `` which contains multiple chapters. Each chapter shall be
147+ sortable inside the book's detail view. The chapter model could look like this:
148+
149+ .. code-block :: python
150+
151+ from django.db import models
152+
153+ class Chapter (models .Model ):
154+ book = models.ForeignKey(
155+ SortableBook,
156+ on_delete = models.CASCADE ,
157+ related_name = ' chapters' ,
158+ )
159+ title = models.CharField(
160+ " Title" ,
161+ max_length = 255 ,
162+ )
163+ my_order = models.PositiveIntegerField(
164+ default = 0 ,
165+ blank = False ,
166+ null = False ,
167+ )
168+
169+ class Meta :
170+ ordering = [' my_order' ]
171+
172+
173+ Then we can create a sortable stacked inline admin interface for the chapter model using:
174+
144175.. code-block :: python
145176
146177 ...
@@ -150,12 +181,14 @@ Example:
150181
151182 class ChapterStackedInline (SortableStackedInline ):
152183 model = Chapter
184+ extra = 1
153185
154186 @admin.register (SortableBook)
155187 class SortableBookAdmin (SortableAdminMixin , admin .ModelAdmin ):
156188 ...
157189 inlines = [ChapterStackedInline]
158190
191+
159192 In case model ``Chapter `` shall be sortable, but model ``Book `` doesn't have to, rewrite the above
160193class as:
161194
@@ -178,7 +211,7 @@ For stacked inlines, the editor for the book's detail view looks like:
178211.. note :: Since version 2.1, two buttons have been added to the draggable area above each inline
179212 form. They serve to move that edited item to the begin or end of the list of inlines.
180213
181- If we instead want to use the tabluar inline class, then modify the code from above to
214+ If we instead want to use the tabular inline class, then modify the code from above to
182215
183216.. code-block :: python
184217
@@ -263,6 +296,11 @@ Setup the Tabular Inlines to enable Buttons to be sorted in Django Admin
263296 class PanelAdmin (SortableAdminBase , admin .ModelAdmin ):
264297 inlines = (ButtonTabularInline,)
265298
299+ If you need a dual list box with a sortable destination list, have a look at my other library
300+ django-formset _ implementing this feature.
301+
302+ .. _django-formset : https://django-formset.fly.dev/dual-selector/#sortable-dual-selector-widget
303+
266304
267305Initial data
268306============
0 commit comments