From e98e5e11a733a6d45a7ac78cc25809605239af6d Mon Sep 17 00:00:00 2001 From: Fabi <18670690+fabi125@users.noreply.github.com> Date: Wed, 1 Apr 2026 12:36:21 -0400 Subject: [PATCH] Fixes #21784: Fix AttributeError when an AnonymousUser tries to sort a table (#21817) --- netbox/netbox/tables/tables.py | 2 +- netbox/netbox/tests/test_tables.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/netbox/netbox/tables/tables.py b/netbox/netbox/tables/tables.py index 41dd3df10..d016cbf28 100644 --- a/netbox/netbox/tables/tables.py +++ b/netbox/netbox/tables/tables.py @@ -159,7 +159,7 @@ class BaseTable(tables.Table): columns = None ordering = None - if self.prefixed_order_by_field in request.GET: + if request.user.is_authenticated and self.prefixed_order_by_field in request.GET: if request.GET[self.prefixed_order_by_field]: # If an ordering has been specified as a query parameter, save it as the # user's preferred ordering for this table. diff --git a/netbox/netbox/tests/test_tables.py b/netbox/netbox/tests/test_tables.py index 730874c49..7ccf7041b 100644 --- a/netbox/netbox/tests/test_tables.py +++ b/netbox/netbox/tests/test_tables.py @@ -1,3 +1,4 @@ +from django.contrib.auth.models import AnonymousUser from django.template import Context, Template from django.test import RequestFactory, TestCase @@ -46,6 +47,16 @@ class BaseTableTest(TestCase): prefetch_lookups = table.data.data._prefetch_related_lookups self.assertEqual(prefetch_lookups, tuple()) + def test_configure_anonymous_user_with_ordering(self): + """ + Verify that table.configure() does not raise an error when an anonymous + user sorts a table column. + """ + request = RequestFactory().get('/?sort=name') + request.user = AnonymousUser() + table = DeviceTable(Device.objects.all()) + table.configure(request) + class TagColumnTable(NetBoxTable): tags = columns.TagColumn(url_name='dcim:site_list')