Minor DRF traceback #6736

Closed
opened 2025-12-29 19:44:47 +01:00 by adam · 5 comments
Owner

Originally created by @ryanmerolle on GitHub (Jul 29, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.2.7

Python version

3.10

Steps to Reproduce

  1. Setup a fresh install of NetBox
  2. Setup logging to debug (may not be required)
  3. Open Django Swagger page
  4. Observe below error in the logs:
default for PrimaryKeyRelatedField(allow_null=True, default=CurrentUserDefault(), queryset=<QuerySet [<User: admin>]>, required=False) is callable but it raised an exception when called; 'default' will not be set on schema
Traceback (most recent call last):
File "/opt/netbox/venv/lib/python3.10/site-packages/drf_yasg/utils.py", line 487, in get_field_default
default = default(field)
File "/opt/netbox/venv/lib/python3.10/site-packages/rest_framework/fields.py", line 290, in __call__
return serializer_field.context['request'].user
KeyError: 'request'
172.24.0.1 - - [28/Jul/2022:16:54:33 +0000] "GET /api/docs/?format=openapi HTTP/1.1" 200 1074260 "http://localhost:8000/api/docs/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"

Expected Behavior

No Error

Observed Behavior

Nothing major, just noise in the logs and possibly a delay in loading the swagger page.

Originally created by @ryanmerolle on GitHub (Jul 29, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.2.7 ### Python version 3.10 ### Steps to Reproduce 1. Setup a fresh install of NetBox 2. Setup logging to debug (may not be required) 3. Open Django Swagger page 4. Observe below error in the logs: ```logs default for PrimaryKeyRelatedField(allow_null=True, default=CurrentUserDefault(), queryset=<QuerySet [<User: admin>]>, required=False) is callable but it raised an exception when called; 'default' will not be set on schema Traceback (most recent call last): File "/opt/netbox/venv/lib/python3.10/site-packages/drf_yasg/utils.py", line 487, in get_field_default default = default(field) File "/opt/netbox/venv/lib/python3.10/site-packages/rest_framework/fields.py", line 290, in __call__ return serializer_field.context['request'].user KeyError: 'request' 172.24.0.1 - - [28/Jul/2022:16:54:33 +0000] "GET /api/docs/?format=openapi HTTP/1.1" 200 1074260 "http://localhost:8000/api/docs/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36" ``` ### Expected Behavior No Error ### Observed Behavior Nothing major, just noise in the logs and possibly a delay in loading the swagger page.
adam added the type: bugstatus: accepted labels 2025-12-29 19:44:47 +01:00
adam closed this issue 2025-12-29 19:44:48 +01:00
Author
Owner

@arthanson commented on GitHub (Aug 26, 2022):

Looks like it is caused by CurrentUserDefault in the following code:

class JournalEntrySerializer(NetBoxModelSerializer):
    ...
    created_by = serializers.PrimaryKeyRelatedField(
        allow_null=True,
        queryset=User.objects.all(),
        required=False,
        default=serializers.CurrentUserDefault()
    )
@arthanson commented on GitHub (Aug 26, 2022): Looks like it is caused by CurrentUserDefault in the following code: ``` class JournalEntrySerializer(NetBoxModelSerializer): ... created_by = serializers.PrimaryKeyRelatedField( allow_null=True, queryset=User.objects.all(), required=False, default=serializers.CurrentUserDefault() ) ```
Author
Owner

@ryanmerolle commented on GitHub (Aug 27, 2022):

Great find @arthanson !

@ryanmerolle commented on GitHub (Aug 27, 2022): Great find @arthanson !
Author
Owner

@amhn commented on GitHub (Aug 31, 2022):

Did look into it. The default function is called for JournalEntrySerializer and WritableJournalEntrySerializer. On the call for WritableJournalEntry the context is empty.

The following diff fixes the traceback, but I am not yet sure if it has other implications or if more variables should be added:

diff --git a/netbox/utilities/custom_inspectors.py b/netbox/utilities/custom_inspectors.py
index 258399e86..3a3ae3f71 100644
--- a/netbox/utilities/custom_inspectors.py
+++ b/netbox/utilities/custom_inspectors.py
@@ -32,9 +32,9 @@ class NetBoxSwaggerAutoSchema(SwaggerAutoSchema):
             if writable_class is not None:
                 if hasattr(serializer, 'child'):
                     child_serializer = self.get_writable_class(serializer.child)
-                    serializer = writable_class(child=child_serializer)
+                    serializer = writable_class(context=serializer.context, child=child_serializer)
                 else:
-                    serializer = writable_class()
+                    serializer = writable_class(context=serializer.context)
         return serializer
 
     def get_writable_class(self, serializer):
@amhn commented on GitHub (Aug 31, 2022): Did look into it. The default function is called for JournalEntrySerializer and WritableJournalEntrySerializer. On the call for WritableJournalEntry the context is empty. The following diff fixes the traceback, but I am not yet sure if it has other implications or if more variables should be added: ``` diff --git a/netbox/utilities/custom_inspectors.py b/netbox/utilities/custom_inspectors.py index 258399e86..3a3ae3f71 100644 --- a/netbox/utilities/custom_inspectors.py +++ b/netbox/utilities/custom_inspectors.py @@ -32,9 +32,9 @@ class NetBoxSwaggerAutoSchema(SwaggerAutoSchema): if writable_class is not None: if hasattr(serializer, 'child'): child_serializer = self.get_writable_class(serializer.child) - serializer = writable_class(child=child_serializer) + serializer = writable_class(context=serializer.context, child=child_serializer) else: - serializer = writable_class() + serializer = writable_class(context=serializer.context) return serializer def get_writable_class(self, serializer): ```
Author
Owner

@arthanson commented on GitHub (Sep 7, 2022):

Marking as blocked as we should figure out #9608 first.

@arthanson commented on GitHub (Sep 7, 2022): Marking as blocked as we should figure out #9608 first.
Author
Owner

@jeremystretch commented on GitHub (Nov 17, 2022):

I tested @amhn's proposed change and the only difference in the schema output was adding a default value of 1 for WritableJournalEntry. Seems like a valid fix to me.

@jeremystretch commented on GitHub (Nov 17, 2022): I tested @amhn's proposed change and the only difference in the schema output was adding a default value of 1 for WritableJournalEntry. Seems like a valid fix to me.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6736