mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-18 13:06:30 -06:00
Closes #3185: Improve performance for custom field access within templates
This commit is contained in:
parent
0804c1acbd
commit
823257ca72
@ -7,6 +7,7 @@
|
|||||||
* [#3138](https://github.com/digitalocean/netbox/issues/3138) - Add 2.5GE and 5GE interface form factors
|
* [#3138](https://github.com/digitalocean/netbox/issues/3138) - Add 2.5GE and 5GE interface form factors
|
||||||
* [#3156](https://github.com/digitalocean/netbox/issues/3156) - Add site link to rack reservations overview
|
* [#3156](https://github.com/digitalocean/netbox/issues/3156) - Add site link to rack reservations overview
|
||||||
* [#3183](https://github.com/digitalocean/netbox/issues/3183) - Enable bulk deletion of sites
|
* [#3183](https://github.com/digitalocean/netbox/issues/3183) - Enable bulk deletion of sites
|
||||||
|
* [#3185](https://github.com/digitalocean/netbox/issues/3185) - Improve performance for custom field access within templates
|
||||||
* [#3186](https://github.com/digitalocean/netbox/issues/3186) - Add interface name filter for IP addresses
|
* [#3186](https://github.com/digitalocean/netbox/issues/3186) - Add interface name filter for IP addresses
|
||||||
|
|
||||||
## Bug Fixes
|
## Bug Fixes
|
||||||
|
@ -102,6 +102,7 @@ class Webhook(models.Model):
|
|||||||
#
|
#
|
||||||
|
|
||||||
class CustomFieldModel(models.Model):
|
class CustomFieldModel(models.Model):
|
||||||
|
_cf = None
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
@ -111,9 +112,12 @@ class CustomFieldModel(models.Model):
|
|||||||
"""
|
"""
|
||||||
Name-based CustomFieldValue accessor for use in templates
|
Name-based CustomFieldValue accessor for use in templates
|
||||||
"""
|
"""
|
||||||
if not hasattr(self, 'get_custom_fields'):
|
if self._cf is None:
|
||||||
return dict()
|
# Cache all custom field values for this instance
|
||||||
return {field.name: value for field, value in self.get_custom_fields().items()}
|
self._cf = {
|
||||||
|
field.name: value for field, value in self.get_custom_fields().items()
|
||||||
|
}
|
||||||
|
return self._cf
|
||||||
|
|
||||||
def get_custom_fields(self):
|
def get_custom_fields(self):
|
||||||
"""
|
"""
|
||||||
@ -126,7 +130,7 @@ class CustomFieldModel(models.Model):
|
|||||||
|
|
||||||
# If the object exists, populate its custom fields with values
|
# If the object exists, populate its custom fields with values
|
||||||
if hasattr(self, 'pk'):
|
if hasattr(self, 'pk'):
|
||||||
values = CustomFieldValue.objects.filter(obj_type=content_type, obj_id=self.pk).select_related('field')
|
values = self.custom_field_values.all()
|
||||||
values_dict = {cfv.field_id: cfv.value for cfv in values}
|
values_dict = {cfv.field_id: cfv.value for cfv in values}
|
||||||
return OrderedDict([(field, values_dict.get(field.pk)) for field in fields])
|
return OrderedDict([(field, values_dict.get(field.pk)) for field in fields])
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user