Region Field for Device is Not Shown Populated When Editing Device Even If It is Actually Set #4213

Closed
opened 2025-12-29 18:33:54 +01:00 by adam · 1 comment
Owner

Originally created by @lastwednesday on GitHub (Oct 26, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: Python 3.8.0 (default, May 7 2020, 02:49:39)
  • NetBox version: 2.9.7

Steps to Reproduce

  1. When given a Device, with the Region set to an existing Region.
    image

  2. Click on the Edit button for the Device, eg to set the Serial, and note that the Region field is now empty.
    image

  3. If the Device is saved, Region field is retained, even though in the edit field it shows as empty.
    image

Expected Behavior

User should expect to see that all fields which are populated in the view mode of the device should also be populated in the edit view of the device.

Observed Behavior

Region field is not showing its correct value when editing the device, despite that there is a value set for it already.

Originally created by @lastwednesday on GitHub (Oct 26, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: Python 3.8.0 (default, May 7 2020, 02:49:39) * NetBox version: 2.9.7 ### Steps to Reproduce 1. When given a Device, with the Region set to an existing Region. ![image](https://user-images.githubusercontent.com/579420/97235009-fb6ef280-17af-11eb-87ee-e031965d29b2.png) 2. Click on the Edit button for the Device, eg to set the Serial, and note that the Region field is now empty. ![image](https://user-images.githubusercontent.com/579420/97235169-4ab52300-17b0-11eb-9141-e7c09f0158a1.png) 3. If the Device is saved, Region field is retained, even though in the edit field it shows as empty. ![image](https://user-images.githubusercontent.com/579420/97235294-8ea82800-17b0-11eb-98b9-8de6386b55d9.png) <!-- What did you expect to happen? --> ### Expected Behavior User should expect to see that all fields which are populated in the view mode of the device should also be populated in the edit view of the device. <!-- What happened instead? --> ### Observed Behavior Region field is not showing its correct value when editing the device, despite that there is a value set for it already.
adam added the type: bugstatus: accepted labels 2025-12-29 18:33:54 +01:00
adam closed this issue 2025-12-29 18:33:54 +01:00
Author
Owner

@jeremystretch commented on GitHub (Nov 3, 2020):

This is really just one instance of a larger need: Automatically populating the value of a parent form field from its child when editing an existing object. Generally, we tackle this within each form, taking care to explicitly set the initial value of each parent field, which in this case is not being done (hence the bug report).

However, a more robust solution would be to extend DynamicModelChoiceMixin to automatically set the initial value based on the existence of a child relationship. For example, because the site field has declared query_params={'region_id': '$region'}, we can infer that that the region field should be set based on the value of site.

Unfortunately, determining the method by which to filter the parent field's queryset isn't always deterministic. For example, we have to filter the region field using the sites reverse relationship, whereas another model might use something like site_set. So, it's probably safer to explicitly declare the relationship on the parent field. Something like the following might suffice:

    region = DynamicModelChoiceField(
        queryset=Region.objects.all(),
        required=False,
        initial_params={
            'sites': '$site',
        }
    )
    site = DynamicModelChoiceField(
        queryset=Site.objects.all(),
        query_params={
            'region_id': '$region'
        }
    )

I'm not committed to the initial_params name specifically, but IMO the approach seems reasonable if perhaps not optimal.

@jeremystretch commented on GitHub (Nov 3, 2020): This is really just one instance of a larger need: Automatically populating the value of a parent form field from its child when editing an existing object. Generally, we tackle this within each form, taking care to explicitly set the initial value of each parent field, which in this case is not being done (hence the bug report). However, a more robust solution would be to extend DynamicModelChoiceMixin to automatically set the initial value based on the existence of a child relationship. For example, because the `site` field has declared `query_params={'region_id': '$region'}`, we can infer that that the `region` field should be set based on the value of `site`. Unfortunately, determining the method by which to filter the parent field's queryset isn't always deterministic. For example, we have to filter the `region` field using the `sites` reverse relationship, whereas another model might use something like `site_set`. So, it's probably safer to explicitly declare the relationship on the parent field. Something like the following might suffice: ```python region = DynamicModelChoiceField( queryset=Region.objects.all(), required=False, initial_params={ 'sites': '$site', } ) site = DynamicModelChoiceField( queryset=Site.objects.all(), query_params={ 'region_id': '$region' } ) ``` I'm not committed to the `initial_params` name specifically, but IMO the approach seems reasonable if perhaps not optimal.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4213