From e2bee049905bab490faa920ec6289459b8748a12 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Thu, 21 Feb 2019 09:56:08 -0500 Subject: [PATCH] Fix sorting for raw_id objects - Added common function to get querystring filters - Excluded querystring parameters used for raw_id fields - Version bump to 2.1.13 - Updated readme --- README.md | 4 ++-- README.rst | 4 ++-- adminsortable/__init__.py | 2 +- adminsortable/admin.py | 21 +++++++++++--------- sample_project/database/test_project.sqlite | Bin 294912 -> 294912 bytes sample_project/samples/admin.py | 6 ++++++ 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 7f3d250..e32cf80 100644 --- a/README.md +++ b/README.md @@ -606,8 +606,8 @@ ordering on top of that just seemed a little much in my opinion. ### Status django-admin-sortable is currently used in production. -### What's new in 2.1.12? -- Fixed multiple list filter issue that was causing incorrect sortable objects to be displayed +### What's new in 2.1.13? +- Fixed an issue when sorting was performed on an object referenced by a raw_id field ### Future - Better template support for foreign keys that are self referential. If someone would like to take on rendering recursive sortables, that would be super. diff --git a/README.rst b/README.rst index 5492dda..ebdc33f 100644 --- a/README.rst +++ b/README.rst @@ -745,10 +745,10 @@ Status django-admin-sortable is currently used in production. -What’s new in 2.1.12? +What’s new in 2.1.13? ~~~~~~~~~~~~~~~~~~~~~ -- Fixed multiple list filter issue that was causing incorrect sortable objects to be displayed +- Fixed an issue when sorting was performed on an object referenced by a raw_id field Future ~~~~~~ diff --git a/adminsortable/__init__.py b/adminsortable/__init__.py index b7dcfef..5e6cb6d 100644 --- a/adminsortable/__init__.py +++ b/adminsortable/__init__.py @@ -1,4 +1,4 @@ -VERSION = (2, 1, 12) +VERSION = (2, 1, 13) DEV_N = None diff --git a/adminsortable/admin.py b/adminsortable/admin.py index 63f641b..f35c93e 100644 --- a/adminsortable/admin.py +++ b/adminsortable/admin.py @@ -34,6 +34,15 @@ class SortableAdminBase(object): after_sorting_js_callback_name = None + def get_querystring_filters(self, request): + filters = {} + + for k, v in request.GET.items(): + if k not in ['_to_field', '_popup',]: + filters.update({ k: v }) + + return filters + def changelist_view(self, request, extra_context=None): """ If the model that inherits Sortable has more than one object, @@ -42,10 +51,7 @@ class SortableAdminBase(object): """ # apply any filters via the querystring - filters = {} - - for k, v in request.GET.items(): - filters.update({ k: v }) + filters = self.get_querystring_filters(request) # Check if the filtered queryset contains more than 1 item # to enable sort link @@ -131,7 +137,8 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): # get sort group index from querystring if present sort_filter_index = request.GET.get('sort_filter') - filters = {} + # apply any filters via the querystring + filters = self.get_querystring_filters(request) if sort_filter_index: try: @@ -139,10 +146,6 @@ class SortableAdmin(SortableAdminBase, ModelAdmin): except (IndexError, ValueError): pass - # apply any filters via the querystring - for k, v in request.GET.items(): - filters.update({ k: v }) - # Apply any sort filters to create a subset of sortable objects return self.get_queryset(request).filter(**filters) diff --git a/sample_project/database/test_project.sqlite b/sample_project/database/test_project.sqlite index 8cfbd34f18e0d2d3f762aa92cd08fbfcc5963605..20b8afae64932f4d83c319d04ae2741a0395b440 100644 GIT binary patch delta 149 zcmZo@5Nc=;njp={HBrWyk*hJGHG#1;fvGitd20g8!gyZ(I}FVHElm6^{73j#ZWc@^ z=(0K7;hQUCw| delta 112 zcmZo@5Nc=;njp={F;T{uk)tu8HG#1;fvGitd20g8!gyW=1_oySm5ls%_z&~1+$@+d zo8Mf5nS()Il97{xGoUCxD>b=9!8pXV7)UaUF#%;nfifI08KaWa;u0nXB}NX0?GN%< G6a)ZqnH%^3 diff --git a/sample_project/samples/admin.py b/sample_project/samples/admin.py index 3761995..8d6bbed 100644 --- a/sample_project/samples/admin.py +++ b/sample_project/samples/admin.py @@ -52,6 +52,12 @@ class WidgetAdmin(SortableAdmin): admin.site.register(Widget, WidgetAdmin) +class CreditAdmin(SortableAdmin): + raw_id_fields = ('project',) + +admin.site.register(Credit, CreditAdmin) + + class CreditInline(SortableTabularInline): model = Credit extra = 1