DynamicModelChoiceField context is ignored when pre-filling fields from request params #9751

Closed
opened 2025-12-29 21:22:05 +01:00 by adam · 2 comments
Owner

Originally created by @samk-acw on GitHub (May 28, 2024).

Deployment Type

Self-hosted

NetBox Version

v4.0.3

Python Version

3.10

Steps to Reproduce

  1. create/edit a form with DynamicModelChoiceField or DynamicModelMultipleChoiceField, set the context attribute to change 'value' to anything that is not 'id'
class SomeForm(NetboxModelForm)
circuit = DynamicModelChoiceField(
    queryset=Circuit.objects.all(),
    context={
        'value': 'cid'
    }
  1. access that form in the browser and prefill the above field with URL params (currently requires using the id)
    /url_for_some_form/?circuit=1

Expected Behavior

the data-value for that input selection should be the value of the field specified in the context attribute.

Observed Behavior

data-value is still set to the ID of the pre-filled object (see highlighted value in screenshot). If you change the selected object the data-value is the expected value.
image

I'm assuming this also applies to ObjectVars in custom scripts as well

Originally created by @samk-acw on GitHub (May 28, 2024). ### Deployment Type Self-hosted ### NetBox Version v4.0.3 ### Python Version 3.10 ### Steps to Reproduce 1. create/edit a form with DynamicModelChoiceField or DynamicModelMultipleChoiceField, set the context attribute to change 'value' to anything that is not 'id' ``` class SomeForm(NetboxModelForm) circuit = DynamicModelChoiceField( queryset=Circuit.objects.all(), context={ 'value': 'cid' } ``` 2. access that form in the browser and prefill the above field with URL params (currently requires using the id) `/url_for_some_form/?circuit=1` ### Expected Behavior the data-value for that input selection should be the value of the field specified in the context attribute. ### Observed Behavior data-value is still set to the ID of the pre-filled object (see highlighted value in screenshot). If you change the selected object the data-value is the expected value. ![image](https://github.com/netbox-community/netbox/assets/55128650/120102d2-3a06-42ba-bbd2-508ed7678e35) I'm assuming this also applies to ObjectVars in custom scripts as well
adam closed this issue 2025-12-29 21:22:06 +01:00
Author
Owner

@jeremystretch commented on GitHub (May 28, 2024):

This is not the function of the context keyword argument on DynamicModelChoiceMixin. Please see the class' docstring and usages within NetBox for reference. If you need further assistance, please open a discussion.

@jeremystretch commented on GitHub (May 28, 2024): This is not the function of the `context` keyword argument on DynamicModelChoiceMixin. Please see the class' docstring and usages within NetBox for reference. If you need further assistance, please open a [discussion](https://github.com/netbox-community/netbox/discussions/new/choose).
Author
Owner

@samk-acw commented on GitHub (May 29, 2024):

Sorry if I'm misunderstanding but how is it not? The context arg allows the value of the input selection value to be changed from ID to another field, which is what I'm trying to do;

Context keys:
value: The name of the attribute which contains the option's value (default: 'id')

But this behavior becomes inconsistent when a DynamicModelChoice field receives initial data from the request query params. The side effect I found is that any other DynamicModelChoice fields that link to the broken one (with the dynamic $ query_params) send the wrong value.

@samk-acw commented on GitHub (May 29, 2024): Sorry if I'm misunderstanding but how is it not? The context arg allows the value of the input selection value to be changed from ID to another field, which is what I'm trying to do; > Context keys: > value: The name of the attribute which contains the option's value (default: 'id') But this behavior becomes inconsistent when a DynamicModelChoice field receives initial data from the request query params. The side effect I found is that any other DynamicModelChoice fields that link to the broken one (with the dynamic $ query_params) send the wrong value.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9751