Added `after_sorting` method to SortableAdminBase class that can be defined on a model admin to be executed after sorting has occurred.

master
Brandon Taylor 2018-07-09 07:49:55 -04:00
parent 3f49a72a6d
commit 6d5f9e97b4
2 changed files with 15 additions and 1 deletions

View File

@ -56,6 +56,11 @@ class SortableAdminBase(object):
return super(SortableAdminBase, self).changelist_view(request, return super(SortableAdminBase, self).changelist_view(request,
extra_context=extra_context) 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): class SortableAdmin(SortableAdminBase, ModelAdmin):
""" """
@ -303,6 +308,8 @@ class SortableAdmin(SortableAdminBase, ModelAdmin):
AttributeError, ValueError): AttributeError, ValueError):
pass pass
self.after_sorting()
return HttpResponse(json.dumps(response, ensure_ascii=False), return HttpResponse(json.dumps(response, ensure_ascii=False),
content_type='application/json') content_type='application/json')

View File

@ -61,6 +61,9 @@ class NoteInline(SortableStackedInline):
model = Note model = Note
extra = 2 extra = 2
def after_sorting(self):
print('I happened after sorting')
class GenericNoteInline(SortableGenericStackedInline): class GenericNoteInline(SortableGenericStackedInline):
model = GenericNote model = GenericNote
@ -83,9 +86,13 @@ class ProjectAdmin(SortableAdmin):
NonSortableCreditInline, NonSortableNoteInline NonSortableCreditInline, NonSortableNoteInline
] ]
list_display = ['__str__', 'category'] list_display = ['__str__', 'category']
list_filter = ('category__title',)
after_sorting_js_callback_name = 'afterSortCallback' after_sorting_js_callback_name = 'afterSortCallback'
sortable_change_list_template = 'adminsortable/custom_change_list.html' 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) admin.site.register(Project, ProjectAdmin)