Using custom validator prevents editing objects #7439

Closed
opened 2025-12-29 20:23:30 +01:00 by adam · 1 comment
Owner

Originally created by @jonasbela on GitHub (Jan 2, 2023).

After implementing the custom validator to check for duplicate serials it prevents me from editing devices because it says Duplicate Serial:

from extras.validators import CustomValidator

class MyValidator(CustomValidator):

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

        if instance.serial and Device.objects.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.

Originally posted by @kkthxbye-code in https://github.com/netbox-community/netbox/issues/9187#issuecomment-1104771333

Originally created by @jonasbela on GitHub (Jan 2, 2023). After implementing the custom validator to check for duplicate serials it prevents me from editing devices because it says Duplicate Serial: ``` from extras.validators import CustomValidator class MyValidator(CustomValidator): def validate(self, instance): from dcim.models import Device if instance.serial and Device.objects.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. _Originally posted by @kkthxbye-code in https://github.com/netbox-community/netbox/issues/9187#issuecomment-1104771333_
adam closed this issue 2025-12-29 20:23:30 +01:00
Author
Owner

@jonasbela commented on GitHub (Jan 2, 2023):

moved to discussion

@jonasbela commented on GitHub (Jan 2, 2023): moved to discussion
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7439