Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jeremystretch opened this issue May 29, 2019 · 1 comment · Fixed by #4608
Closed

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

jeremystretch opened this issue May 29, 2019 · 1 comment · Fixed by #4608
Assignees
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user
Milestone

Comments

@jeremystretch
Copy link
Member

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.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user labels May 29, 2019
@jeremystretch jeremystretch changed the title Introduce a CustomFieldManager to cache custom fields for an object Introduce a CustomFieldManager to cache custom fields for each content type May 29, 2019
@jeremystretch jeremystretch added this to the v2.9 milestone Mar 11, 2020
@paravoid
Copy link
Contributor

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 :)

@jeremystretch jeremystretch self-assigned this May 7, 2020
jeremystretch added a commit that referenced this issue May 8, 2020
Closes #3226: Implement a custom manager for CustomField
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants