Browse Source

Fixes #1358: Correct VRF example values in IP/prefix import forms

Jeremy Stretch 7 years ago
parent
commit
106627da04

+ 1 - 1
netbox/templates/utilities/obj_import.html

@@ -44,7 +44,7 @@
                         <td>
                             {{ field.help_text|default:field.label }}
                             {% if field.choices %}
-                                <br /><small class="text-muted">Choices: {{ field.choices|example_choices }}</small>
+                                <br /><small class="text-muted">Choices: {{ field|example_choices }}</small>
                             {% elif field|widget_type == 'dateinput' %}
                                 <br /><small class="text-muted">Format: YYYY-MM-DD</small>
                             {% elif field|widget_type == 'checkboxinput' %}

+ 11 - 7
netbox/utilities/templatetags/helpers.py

@@ -63,19 +63,23 @@ def bettertitle(value):
 
 
 @register.filter()
-def example_choices(value, arg=3):
+def example_choices(field, arg=3):
     """
     Returns a number (default: 3) of example choices for a ChoiceFiled (useful for CSV import forms).
     """
-    choices = []
-    for id, label in value:
-        if len(choices) == arg:
-            choices.append('etc.')
+    examples = []
+    if hasattr(field, 'queryset'):
+        choices = [(obj.pk, getattr(obj, field.to_field_name)) for obj in field.queryset[:arg+1]]
+    else:
+        choices = field.choices
+    for id, label in choices:
+        if len(examples) == arg:
+            examples.append('etc.')
             break
         if not id:
             continue
-        choices.append(label)
-    return ', '.join(choices) or 'None'
+        examples.append(label)
+    return ', '.join(examples) or 'None'
 
 
 #