mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Adds hide-if-unset to custom field (#12723)
* adds hide-if-unset to custom field #12597 * moved hide logic from template to python * fix indentation * Update logic for omit_hidden under get_custom_fields() * Update docs * Account for False values --------- Co-authored-by: jeremystretch <jstretch@netboxlabs.com>
This commit is contained in:
committed by
GitHub
parent
9b9a559e0c
commit
d7ca453f26
@ -68,11 +68,12 @@ Defines how filters are evaluated against custom field values.
|
||||
|
||||
Controls how and whether the custom field is displayed within the NetBox user interface.
|
||||
|
||||
| Option | Description |
|
||||
|------------|--------------------------------------|
|
||||
| Read/write | Display and permit editing (default) |
|
||||
| Read-only | Display field but disallow editing |
|
||||
| Hidden | Do not display field in the UI |
|
||||
| Option | Description |
|
||||
|-------------------|--------------------------------------------------|
|
||||
| Read/write | Display and permit editing (default) |
|
||||
| Read-only | Display field but disallow editing |
|
||||
| Hidden | Do not display field in the UI |
|
||||
| Hidden (if unset) | Display in the UI only when a value has been set |
|
||||
|
||||
### Default
|
||||
|
||||
|
@ -56,11 +56,13 @@ class CustomFieldVisibilityChoices(ChoiceSet):
|
||||
VISIBILITY_READ_WRITE = 'read-write'
|
||||
VISIBILITY_READ_ONLY = 'read-only'
|
||||
VISIBILITY_HIDDEN = 'hidden'
|
||||
VISIBILITY_HIDDEN_IFUNSET = 'hidden-ifunset'
|
||||
|
||||
CHOICES = (
|
||||
(VISIBILITY_READ_WRITE, 'Read/Write'),
|
||||
(VISIBILITY_READ_ONLY, 'Read-only'),
|
||||
(VISIBILITY_HIDDEN, 'Hidden'),
|
||||
(VISIBILITY_HIDDEN_IFUNSET, 'Hidden (if unset)'),
|
||||
)
|
||||
|
||||
|
||||
|
@ -197,11 +197,15 @@ class CustomFieldsMixin(models.Model):
|
||||
data = {}
|
||||
|
||||
for field in CustomField.objects.get_for_model(self):
|
||||
# Skip fields that are hidden if 'omit_hidden' is set
|
||||
if omit_hidden and field.ui_visibility == CustomFieldVisibilityChoices.VISIBILITY_HIDDEN:
|
||||
continue
|
||||
|
||||
value = self.custom_field_data.get(field.name)
|
||||
|
||||
# Skip fields that are hidden if 'omit_hidden' is set
|
||||
if omit_hidden:
|
||||
if field.ui_visibility == CustomFieldVisibilityChoices.VISIBILITY_HIDDEN:
|
||||
continue
|
||||
if field.ui_visibility == CustomFieldVisibilityChoices.VISIBILITY_HIDDEN_IFUNSET and not value:
|
||||
continue
|
||||
|
||||
data[field] = field.deserialize(value)
|
||||
|
||||
return data
|
||||
@ -227,6 +231,8 @@ class CustomFieldsMixin(models.Model):
|
||||
|
||||
for cf in visible_custom_fields:
|
||||
value = self.custom_field_data.get(cf.name)
|
||||
if value in (None, []) and cf.ui_visibility == CustomFieldVisibilityChoices.VISIBILITY_HIDDEN_IFUNSET:
|
||||
continue
|
||||
value = cf.deserialize(value)
|
||||
groups[cf.group_name][cf] = value
|
||||
|
||||
|
Reference in New Issue
Block a user