mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-14 07:42:18 -06:00
* Closes #8198: Implement ability to enforce custom field uniqueness * Add missing form fields & table columns for validation attributes * Remove obsolete code
This commit is contained in:
@@ -180,6 +180,11 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
|
||||
'example, <code>^[A-Z]{3}$</code> will limit values to exactly three uppercase letters.'
|
||||
)
|
||||
)
|
||||
validation_unique = models.BooleanField(
|
||||
verbose_name=_('must be unique'),
|
||||
default=False,
|
||||
help_text=_('The value of this field must be unique for the assigned object')
|
||||
)
|
||||
choice_set = models.ForeignKey(
|
||||
to='CustomFieldChoiceSet',
|
||||
on_delete=models.PROTECT,
|
||||
@@ -217,7 +222,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
|
||||
clone_fields = (
|
||||
'object_types', 'type', 'related_object_type', 'group_name', 'description', 'required', 'search_weight',
|
||||
'filter_logic', 'default', 'weight', 'validation_minimum', 'validation_maximum', 'validation_regex',
|
||||
'choice_set', 'ui_visible', 'ui_editable', 'is_cloneable',
|
||||
'validation_unique', 'choice_set', 'ui_visible', 'ui_editable', 'is_cloneable',
|
||||
)
|
||||
|
||||
class Meta:
|
||||
@@ -334,6 +339,12 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
|
||||
'validation_regex': _("Regular expression validation is supported only for text and URL fields")
|
||||
})
|
||||
|
||||
# Uniqueness can not be enforced for boolean fields
|
||||
if self.validation_unique and self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
||||
raise ValidationError({
|
||||
'validation_unique': _("Uniqueness cannot be enforced for boolean fields")
|
||||
})
|
||||
|
||||
# Choice set must be set on selection fields, and *only* on selection fields
|
||||
if self.type in (
|
||||
CustomFieldTypeChoices.TYPE_SELECT,
|
||||
|
||||
Reference in New Issue
Block a user