Object custom field data should return instances for object and multi-object custom fields #6826

Closed
opened 2025-12-29 19:45:49 +01:00 by adam · 4 comments
Owner

Originally created by @abhi1693 on GitHub (Aug 17, 2022).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

3.2.9

Feature type

Change to existing functionality

Proposed functionality

I think it could need a change for the CustomFieldMixin to work. Because get_custom_fields returns a dict where the key is the object and not the field name.

Use case

I am trying to build a custom link URL which requires querying custom object attributes. I want to access the site.slug on the custom object assigned to ClusterGroup via a plugin model, something like

object.get_custom_fields()['field_name'].site.slug

Database changes

No response

External dependencies

No response

Originally created by @abhi1693 on GitHub (Aug 17, 2022). Originally assigned to: @jeremystretch on GitHub. ### NetBox version 3.2.9 ### Feature type Change to existing functionality ### Proposed functionality I think it could need a change for the `CustomFieldMixin` to work. Because `get_custom_fields` returns a `dict` where the key is the `object` and not the `field name`. ### Use case I am trying to build a custom link URL which requires querying custom object attributes. I want to access the `site.slug` on the custom object assigned to ClusterGroup via a plugin model, something like ``` object.get_custom_fields()['field_name'].site.slug ``` ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: feature labels 2025-12-29 19:45:49 +01:00
adam closed this issue 2025-12-29 19:45:49 +01:00
Author
Owner

@jeremystretch commented on GitHub (Aug 18, 2022):

Unpacking the specific use, it seems the goal here is for object.cf to return a properly deserialized version of object and multi-object fields (rather than the PK or list of PKs, respectively). This should be doable, I think, by running custom_field_data through deserialize() for each field.

This would technically be a breaking change, however I can also see an argument that this is more of bug fix than a feature request.

@jeremystretch commented on GitHub (Aug 18, 2022): Unpacking the specific use, it seems the goal here is for `object.cf` to return a properly deserialized version of object and multi-object fields (rather than the PK or list of PKs, respectively). This should be doable, I think, by running `custom_field_data` through `deserialize()` for each field. This would technically be a breaking change, however I can also see an argument that this is more of bug fix than a feature request.
Author
Owner

@renatoalmeidaoliveira commented on GitHub (Aug 19, 2022):

I guess the main problem here is that cf returns the pk and the get_custom_fields returns a dict with the keys equals the object and not the custom field name.
One option could be create a new method that returns a dict with keys equals the field name and the value the object or list of objects, like get_custom_relations that way there're no major changes in the core code.

@renatoalmeidaoliveira commented on GitHub (Aug 19, 2022): I guess the main problem here is that cf returns the pk and the get_custom_fields returns a dict with the keys equals the object and not the custom field name. One option could be create a new method that returns a dict with keys equals the field name and the value the object or list of objects, like get_custom_relations that way there're no major changes in the core code.
Author
Owner

@github-actions[bot] commented on GitHub (Oct 18, 2022):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Oct 18, 2022): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

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

I've tweaked the cf attribute so that it now returns deserialized values (i.e. objects instead of their IDs):

>>> tenant=Tenant.objects.get(pk=5)
>>> tenant.cf
{'primary_site': <Site: DM-NYC>, 'cust_id': 'DMI01', 'is_active': True}
@jeremystretch commented on GitHub (Nov 4, 2022): I've tweaked the `cf` attribute so that it now returns deserialized values (i.e. objects instead of their IDs): ``` >>> tenant=Tenant.objects.get(pk=5) >>> tenant.cf {'primary_site': <Site: DM-NYC>, 'cust_id': 'DMI01', 'is_active': True} ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6826