1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

More performance improvements

This commit is contained in:
Jeremy Stretch
2016-08-17 13:40:22 -04:00
parent b7a90dd09a
commit b0a325f173

View File

@ -5,7 +5,9 @@ from .models import CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_INTEGER, CF_TYPE_SELE
def get_custom_fields_for_model(content_type, bulk_editing=False): def get_custom_fields_for_model(content_type, bulk_editing=False):
"""Retrieve all CustomFields applicable to the given ContentType""" """
Retrieve all CustomFields applicable to the given ContentType
"""
field_dict = {} field_dict = {}
custom_fields = CustomField.objects.filter(obj_type=content_type) custom_fields = CustomField.objects.filter(obj_type=content_type)
@ -63,9 +65,11 @@ class CustomFieldForm(forms.ModelForm):
super(CustomFieldForm, self).__init__(*args, **kwargs) super(CustomFieldForm, self).__init__(*args, **kwargs)
# Add all applicable CustomFields to the form # Add all applicable CustomFields to the form
custom_fields = []
for name, field in get_custom_fields_for_model(self.obj_type).items(): for name, field in get_custom_fields_for_model(self.obj_type).items():
self.fields[name] = field self.fields[name] = field
self.custom_fields.append(name) custom_fields.append(name)
self.custom_fields = custom_fields
# If editing an existing object, initialize values for all custom fields # If editing an existing object, initialize values for all custom fields
if self.instance.pk: if self.instance.pk:
@ -78,7 +82,8 @@ class CustomFieldForm(forms.ModelForm):
for field_name in self.custom_fields: for field_name in self.custom_fields:
try: try:
cfv = CustomFieldValue.objects.get(field=self.fields[field_name].model, obj_type=self.obj_type, cfv = CustomFieldValue.objects.select_related('field').get(field=self.fields[field_name].model,
obj_type=self.obj_type,
obj_id=self.instance.pk) obj_id=self.instance.pk)
except CustomFieldValue.DoesNotExist: except CustomFieldValue.DoesNotExist:
cfv = CustomFieldValue( cfv = CustomFieldValue(
@ -116,5 +121,4 @@ class CustomFieldBulkEditForm(forms.Form):
field.required = False field.required = False
self.fields[name] = field self.fields[name] = field
custom_fields.append(name) custom_fields.append(name)
self.custom_fields = custom_fields self.custom_fields = custom_fields