Skip to content

Commit

Permalink
Added SortableHiddenMixin to copy a useful shortcut from Grappelli
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenhall authored and fdintino committed Apr 27, 2019
1 parent d3749e2 commit 4e873b0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -5,9 +5,14 @@ Changelog

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

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

Expand Down
22 changes: 22 additions & 0 deletions docs/customization.rst
Expand Up @@ -9,6 +9,28 @@ Sortables

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.

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``.

.. code-block:: python
import nested_admin
from django.contrib import admin
from .models import TableOfContents, TocArticle, TocSection
class TocArticleInline(nested_admin.SortableHiddenMixin,
nested_admin.NestedStackedInline):
model = TocArticle
class TocSectionInline(nested_admin.SortableHiddenMixin,
nested_admin.NestedStackedInline):
model = TocSection
inlines = [TocArticleInline]
@admin.register(TableOfContents)
class TableOfContentsAdmin(nested_admin.NestedModelAdmin):
inlines = [TocSectionInline]
Events
======

Expand Down
4 changes: 3 additions & 1 deletion nested_admin/__init__.py
Expand Up @@ -22,6 +22,8 @@

# import mapping to objects in other modules
all_by_module = {
'nested_admin.forms': (
'SortableHiddenMixin'),
'nested_admin.formsets': (
'NestedInlineFormSet', 'NestedBaseGenericInlineFormSet'),
'nested_admin.nested': (
Expand All @@ -42,7 +44,7 @@
'NestedGenericStackedPolymorphicInline')

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

object_origins = {}
for module, items in all_by_module.items():
Expand Down
18 changes: 18 additions & 0 deletions nested_admin/forms.py
@@ -0,0 +1,18 @@
from django.forms.widgets import HiddenInput


class SortableHiddenMixin(object):
"""
Enables inline sorting and hides the sortable field.
By default it assumes the field name is ``position``. This can be
overridden by setting the ``sortable_field_name`` attribute to a
different value.
"""
sortable_field_name = "position"

def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == self.sortable_field_name:
kwargs["widget"] = HiddenInput()
return super(SortableHiddenMixin, self).formfield_for_dbfield(
db_field, **kwargs)

1 comment on commit 4e873b0

@grnstv
Copy link

@grnstv grnstv commented on 4e873b0 Aug 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice feature, please mention in docs how to call it properly:
nested_admin.**forms**.SortableHiddenMixin

Please sign in to comment.