Skip to content

Commit 4e873b0

Browse files
brandenhallfdintino
authored andcommitted
Added SortableHiddenMixin to copy a useful shortcut from Grappelli
1 parent d3749e2 commit 4e873b0

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ Changelog
55

66
* Fixed: visual inconsistencies in grappelli tabular inline styles (`#136`_)
77
* Fixed: numerous issues with django-polymorphic integration (`#138`_)
8+
* Feature: Added ``SortableHiddenMixin`` akin to grappelli’s
9+
`GrappelliSortableHiddenMixin`_ (`#123`_). Thanks `@brandenhall`_!
810

911
.. _#136: https://github.com/theatlantic/django-nested-admin/issues/136
1012
.. _#138: https://github.com/theatlantic/django-nested-admin/issues/138
13+
.. _GrappelliSortableHiddenMixin: https://django-grappelli.readthedocs.io/en/2.12.2/customization.html#grappellisortablehiddenmixin
14+
.. _#123: https://github.com/theatlantic/django-nested-admin/pull/123
15+
.. @brandenhall_: https://github.com/brandenhall
1116
1217
**3.2.2 (Apr 9, 2019)**
1318

docs/customization.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ Sortables
99

1010
The steps for enabling drag-and-drop sorting functionality is identical to Grappelli’s (even with the vanilla Django admin), so the `Grappelli documentation <http://django-grappelli.readthedocs.org/en/latest/customization.html#inline-sortables>`_ for this feature is the best reference. This is equally true of sortables in Django Suit—do not follow Django Suit’s instructions for sortables, as they will not work. The `other features of Grappelli <http://django-grappelli.readthedocs.org/en/latest/customization.html>`_ have not been ported to work with vanilla Django, but in principle they should all still work with nested inlines using django-nested-admin if Grappelli is installed. If you run into any difficulty with this, please `create an issue <https://github.com/theatlantic/django-nested-admin/issues>`_ on this project’s Github.
1111

12+
If you want to have a sortable inline and also want to hide the field you're sorting, you can use ``SortableHiddenMixin``. Note that this mixin sets ``sortable_field_name`` to ``position`` by default (though this can be overridden by setting ``sortable_field_name`` on your inline class) and must be inherited prior to ``NestedStackedInline`` or ``NestedTabularInline``.
13+
14+
.. code-block:: python
15+
16+
import nested_admin
17+
from django.contrib import admin
18+
from .models import TableOfContents, TocArticle, TocSection
19+
20+
class TocArticleInline(nested_admin.SortableHiddenMixin,
21+
nested_admin.NestedStackedInline):
22+
model = TocArticle
23+
24+
class TocSectionInline(nested_admin.SortableHiddenMixin,
25+
nested_admin.NestedStackedInline):
26+
model = TocSection
27+
inlines = [TocArticleInline]
28+
29+
@admin.register(TableOfContents)
30+
class TableOfContentsAdmin(nested_admin.NestedModelAdmin):
31+
inlines = [TocSectionInline]
32+
33+
1234
Events
1335
======
1436

nested_admin/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
# import mapping to objects in other modules
2424
all_by_module = {
25+
'nested_admin.forms': (
26+
'SortableHiddenMixin'),
2527
'nested_admin.formsets': (
2628
'NestedInlineFormSet', 'NestedBaseGenericInlineFormSet'),
2729
'nested_admin.nested': (
@@ -42,7 +44,7 @@
4244
'NestedGenericStackedPolymorphicInline')
4345

4446
# modules that should be imported when accessed as attributes of nested_admin
45-
attribute_modules = frozenset(['formsets', 'nested', 'polymorphic'])
47+
attribute_modules = frozenset(['forms', 'formsets', 'nested', 'polymorphic'])
4648

4749
object_origins = {}
4850
for module, items in all_by_module.items():

nested_admin/forms.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.forms.widgets import HiddenInput
2+
3+
4+
class SortableHiddenMixin(object):
5+
"""
6+
Enables inline sorting and hides the sortable field.
7+
8+
By default it assumes the field name is ``position``. This can be
9+
overridden by setting the ``sortable_field_name`` attribute to a
10+
different value.
11+
"""
12+
sortable_field_name = "position"
13+
14+
def formfield_for_dbfield(self, db_field, **kwargs):
15+
if db_field.name == self.sortable_field_name:
16+
kwargs["widget"] = HiddenInput()
17+
return super(SortableHiddenMixin, self).formfield_for_dbfield(
18+
db_field, **kwargs)

0 commit comments

Comments
 (0)