Skip to content

Sorting Auditlog by actor causes 500 error #3581

Closed

Description

@Simrayz

Describe the bug

When testing the Auditlog tool, I discovered that sorting the table by "Actor" results in a 500 error.

Console output

GET http://localhost:8001/api/1/auditlog/?page=1&search=&page_size=10&ordering=actor&_=1760084095777 500 (Internal Server Error)

Network tab

FieldError at /api/1/auditlog/
Field 'actor' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.

To Reproduce

  1. Open the "Auditlog" tool
  2. Try to sort the table by "Actor"
  3. You should get an an alert about an "Ajax error"

Expected behavior

The table should sort without issue.

Screenshots

Image

Tracebacks

[Fri Oct 10 10:18:12 2025] [ERROR] [pid=23979 django.request] Internal Server Error: /api/1/auditlog/
Traceback (most recent call last):
  File "/home/vscode/.venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 515, in dispatch
    response = self.handle_exception(exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 475, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 486, in raise_uncaught_exception
    raise exc
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/views.py", line 512, in dispatch
    response = handler(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/mixins.py", line 38, in list
    queryset = self.filter_queryset(self.get_queryset())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/generics.py", line 154, in filter_queryset
    queryset = backend().filter_queryset(self.request, queryset, self)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/rest_framework/filters.py", line 327, in filter_queryset
    return queryset.order_by(*ordering)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.venv/lib/python3.11/site-packages/django/db/models/query.py", line 1659, in order_by
    obj.query.add_ordering(*field_names)
  File "/home/vscode/.venv/lib/python3.11/site-packages/django/db/models/sql/query.py", line 2222, in add_ordering
    self.names_to_path(item.split(LOOKUP_SEP), self.model._meta)
  File "/home/vscode/.venv/lib/python3.11/site-packages/django/db/models/sql/query.py", line 1701, in names_to_path
    raise FieldError(
django.core.exceptions.FieldError: Field 'actor' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.
[10/Oct/2025 10:18:12] "GET /api/1/auditlog/?page=1&search=&page_size=10&ordering=actor&_=1760084095779 HTTP/1.1" 500 136292

Environment (please complete the following information):

  • MacOS, Brave 1.83
  • Run in local development, from master branch. Also present in 5.14.x and 5.13.x.

Additional context

Activity

  1. added theissue type on Oct 10, 2025
  2. changed the title [-][BUG][/-] [+]Sorting Auditlog by actor causes 500 error[/+] on Oct 10, 2025
  3. removed their assignment
    on Dec 3, 2025
  4. johannaengland commented on Dec 16, 2025

    @johannaengland
    Contributor

    I have worked on this before, but didn't have the time to actually fix it, but I wrote a test for it:
    Adding to tests/integration/api_test.py:

    class TestLogEntryViewSet:
        def test_order_by_actor_should_not_fail(
            self, auditlog_endpoint, api_client, serializer_models
        ):
            response = api_client.get(f"{ENDPOINTS[auditlog_endpoint]}?ordering=actor")
            assert response.status_code == 200
            assert response.data['id'] == netboxentity.id
    

    and

    @pytest.fixture()
    def auditlog_endpoint(db, token):
        endpoint = 'auditlog'
        create_token_endpoint(token, endpoint)
        return endpoint
    
  5. self-assigned this
    on Dec 16, 2025
  6. linked a pull request that will close this issueFix auditlog actor sorting #3706on Dec 16, 2025
  7. linked a pull request that will close this issueDisable auditlog actor sort #3708on Dec 16, 2025
  8. aleksfl commented on Dec 18, 2025

    @aleksfl
    Contributor

    Current workaround disables sorting by actor #3708 . In the future, sorting can hopefully be fixed for all LegacyGenericForeignKeys.

  9. removed their assignment
    on Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions