Enforce Unique Serial Numbers #6387

Closed
opened 2025-12-29 19:40:06 +01:00 by adam · 4 comments
Owner

Originally created by @lamoni on GitHub (Apr 20, 2022).

NetBox version

v3.2.1

Feature type

Change to existing functionality

Proposed functionality

I think an option to enforce unique serial numbers on devices would be useful. I imagine the behavior would be similar to the old strict hostname uniqueness that used to be enforced (i.e. if the serial number is blank, that's an "allowed" duplicate, but if the serial number is non-NULL, then it has to be unique).

Use case

Right now, we currently have our own external auditing script that checks for duplicate serial numbers and corrects them / flags them to the users/teams that initially imported them. It would be useful for us to stop those from ever being imported in the first place. Given the fact the serial number can (usually...) be relied upon as a "unique identifier", I feel like it would make sense to at least have the option to enforce uniqueness, similar to the ENFORCE_GLOBAL_UNIQUE option for IP/prefixes.

Database changes

None that I know of.

External dependencies

None that I know of.

Originally created by @lamoni on GitHub (Apr 20, 2022). ### NetBox version v3.2.1 ### Feature type Change to existing functionality ### Proposed functionality I think an option to enforce unique serial numbers on devices would be useful. I imagine the behavior would be similar to the old strict hostname uniqueness that used to be enforced (i.e. if the serial number is blank, that's an "allowed" duplicate, but if the serial number is non-NULL, then it has to be unique). ### Use case Right now, we currently have our own external auditing script that checks for duplicate serial numbers and corrects them / flags them to the users/teams that initially imported them. It would be useful for us to stop those from ever being imported in the first place. Given the fact the serial number can (usually...) be relied upon as a "unique identifier", I feel like it would make sense to at least have the option to enforce uniqueness, similar to the ENFORCE_GLOBAL_UNIQUE option for IP/prefixes. ### Database changes None that I know of. ### External dependencies None that I know of.
adam added the type: feature label 2025-12-29 19:40:06 +01:00
adam closed this issue 2025-12-29 19:40:06 +01:00
Author
Owner

@pgnuta commented on GitHub (Apr 21, 2022):

Could you use custom validation for this instead?
https://demo.netbox.dev/static/docs/customization/custom-validation/

@pgnuta commented on GitHub (Apr 21, 2022): Could you use custom validation for this instead? https://demo.netbox.dev/static/docs/customization/custom-validation/
Author
Owner

@lamoni commented on GitHub (Apr 21, 2022):

@pgnuta thanks! I've been going over examples I can find online, and they all seem to only reference the instance that is being created (i.e. I can do static validation like my own custom regex or logic. I think for our use case, we would need to be able to reference all of the serial numbers for any of the devices so we can check if the new serial number is already in that list? Please correct me if I am misunderstanding. Thanks!

@lamoni commented on GitHub (Apr 21, 2022): @pgnuta thanks! I've been going over examples I can find online, and they all seem to only reference the instance that is being created (i.e. I can do static validation like my own custom regex or logic. I think for our use case, we would need to be able to reference all of the serial numbers for any of the devices so we can check if the new serial number is already in that list? Please correct me if I am misunderstanding. Thanks!
Author
Owner

@kkthxbye-code commented on GitHub (Apr 21, 2022):

He means something like this:

from extras.validators import CustomValidator

class MyValidator(CustomValidator):

    def validate(self, instance):
        from dcim.models import Device

        if instance.serial and Device.objects.exclude(pk=instance.pk).filter(serial=instance.serial).exists():
            self.fail("Duplicate serial", field='serial')
CUSTOM_VALIDATORS = {
    'dcim.device': (
        MyValidator(),
    )
}

This is tested and working. In this example the validator is defined in the configuration.py file itself. There are other ways to organize it.

@kkthxbye-code commented on GitHub (Apr 21, 2022): He means something like this: ``` from extras.validators import CustomValidator class MyValidator(CustomValidator): def validate(self, instance): from dcim.models import Device if instance.serial and Device.objects.exclude(pk=instance.pk).filter(serial=instance.serial).exists(): self.fail("Duplicate serial", field='serial') ``` ``` CUSTOM_VALIDATORS = { 'dcim.device': ( MyValidator(), ) } ``` This is tested and working. In this example the validator is defined in the configuration.py file itself. There are other ways to organize it.
Author
Owner

@lamoni commented on GitHub (Apr 22, 2022):

@kkthxbye-code Ah perfect, thank you! And thank you as well, @pgnuta. Closing this

@lamoni commented on GitHub (Apr 22, 2022): @kkthxbye-code Ah perfect, thank you! And thank you as well, @pgnuta. Closing this
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6387