Skip to content

Commit

Permalink
Added after_sorting method to SortableAdminBase class that can be d…
Browse files Browse the repository at this point in the history
…efined on a model admin to be executed after sorting has occurred.
  • Loading branch information
alsoicode committed Jul 9, 2018
1 parent 3f49a72 commit 6d5f9e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions adminsortable/admin.py
Expand Up @@ -56,6 +56,11 @@ def changelist_view(self, request, extra_context=None):
return super(SortableAdminBase, self).changelist_view(request,
extra_context=extra_context)

# override this function in your SortableAdmin if you need to do something
# after sorting has occurred
def after_sorting(self):
pass


class SortableAdmin(SortableAdminBase, ModelAdmin):
"""
Expand Down Expand Up @@ -303,6 +308,8 @@ def do_sorting_view(self, request, model_type_id=None):
AttributeError, ValueError):
pass

self.after_sorting()

return HttpResponse(json.dumps(response, ensure_ascii=False),
content_type='application/json')

Expand Down
9 changes: 8 additions & 1 deletion sample_project/samples/admin.py
Expand Up @@ -61,6 +61,9 @@ class NoteInline(SortableStackedInline):
model = Note
extra = 2

def after_sorting(self):
print('I happened after sorting')


class GenericNoteInline(SortableGenericStackedInline):
model = GenericNote
Expand All @@ -83,9 +86,13 @@ class ProjectAdmin(SortableAdmin):
NonSortableCreditInline, NonSortableNoteInline
]
list_display = ['__str__', 'category']
list_filter = ('category__title',)
after_sorting_js_callback_name = 'afterSortCallback'
sortable_change_list_template = 'adminsortable/custom_change_list.html'
sortable_change_form_template = "adminsortable/custom_change_form.html"
sortable_change_form_template = 'adminsortable/custom_change_form.html'

def after_sorting(self):
print('I happened after sorting')

admin.site.register(Project, ProjectAdmin)

Expand Down

0 comments on commit 6d5f9e9

Please sign in to comment.