Introduce a CustomFieldManager to cache custom fields for each content type #2640

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

Originally created by @jeremystretch on GitHub (May 29, 2019).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

Currently, retrieving the custom fields for an object is done by calling get_custom_fields() on that object. This method begins by finding all CustomFields assigned to the object's ContentType:

fields = CustomField.objects.filter(obj_type=content_type)

As pointed out in #3185, this results in a new database query for each call. This can be highly inefficient, for example, when accessing the custom fields for each device in a list of devices.

We should implement a CustomFieldManager similar to Django's ContentTypeManager which would allow us to cache the available custom fields for each model.

Justification

Utilizing a manager which caches the custom fields for each content type will avoid redundant database access and greatly improve performance where custom fields are accessed in export templates and reports. It will likely also simplify CustomFieldModelSerializer.

Originally created by @jeremystretch on GitHub (May 29, 2019). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes Currently, retrieving the custom fields for an object is done by calling `get_custom_fields()` on that object. This method begins by finding all CustomFields assigned to the object's ContentType: ``` fields = CustomField.objects.filter(obj_type=content_type) ``` As pointed out in #3185, this results in a new database query for each call. This can be highly inefficient, for example, when accessing the custom fields for each device in a list of devices. We should implement a CustomFieldManager similar to Django's [ContentTypeManager](https://github.com/django/django/blob/master/django/contrib/contenttypes/models.py#L8) which would allow us to cache the available custom fields for each model. ### Justification Utilizing a manager which caches the custom fields for each content type will avoid redundant database access and greatly improve performance where custom fields are accessed in export templates and reports. It will likely also simplify CustomFieldModelSerializer.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:20:46 +01:00
adam closed this issue 2025-12-29 18:20:46 +01:00
Author
Owner

@paravoid commented on GitHub (Mar 27, 2020):

Awesome to see this as part of v2.9 milestone! :)

Are you envisioning this to also address #3083 once implemented or is this out of scope? It'd be neat to expand custom fields to more models -- as I mentioned in that issue, I am particularly interested in custom fields for inventory items to track some asset information for some of the more expensive (5-6 figures) inventory items (Juniper FPCs, REs etc.).

Also, in that issue I also pitched maybe using JSONField (backed by Postgres' jsonb) for custom fields, to allow for schema-free fields but with efficient indexing and searching. I had played around a little bit with that for Netbox, and can give it another stab once the CustomFieldManager functionality gets implemented :)

@paravoid commented on GitHub (Mar 27, 2020): Awesome to see this as part of v2.9 milestone! :) Are you envisioning this to also address #3083 once implemented or is this out of scope? It'd be neat to expand custom fields to more models -- as I mentioned in that issue, I am particularly interested in custom fields for inventory items to track some asset information for some of the more expensive (5-6 figures) inventory items (Juniper FPCs, REs etc.). Also, in that issue I also pitched maybe using JSONField (backed by Postgres' jsonb) for custom fields, to allow for schema-free fields but with efficient indexing and searching. I had played around a little bit with that for Netbox, and can give it another stab once the CustomFieldManager functionality gets implemented :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2640