Custom Script variable query by current username #22845
|
Hi NetBox community, I want to write a custom script and want to set the The NetBox user should have an vm_owner = ObjectVar(
label="VM ownership",
description="The owner of the VM",
model=Owner,
query_params={
'user': request.user.username
},
required=True)In the I know, that I can access the username using |
Replies: 2 comments
|
Hi @IsarNet-Joe, The The issue here is the point at which script variables are created. They are defined as class attributes when the script module is loaded. At that time, there is no active HTTP request or
One other detail to keep in mind is that the As a workaround, you could perform the membership check inside the script’s This would not filter the available choices in the form, but it would ensure that the selected Owner is validated before the script makes any changes. |
|
Thank you @pheus for the detailed answer. Will follow your advise and catch the exception inside the |
Hi @IsarNet-Joe,
The
userfilter is available and accepts a username, so a static value such asquery_params={'user': 'alice'}should work as expected.The issue here is the point at which script variables are created. They are defined as class attributes when the script module is loaded. At that time, there is no active HTTP request or
Scriptinstance, so neitherrequest.usernorself.requestis available. NetBox assignsself.requestonly when the submitted script starts running, after the form has already been created, displayed, and validated.query_paramscurrently supports static values and references to other script fields using$field_name, but it cannot dynamically resolve the cur…