netbox
netbox copied to clipboard
15029 check if duplicate FHRP group assignment
Fixes: #15029
Check for duplicate FHRP group assignment so IntegrityError isn't raised.
Have you been able to determine why this exception isn't caught, whereas others are? For instance, attempting to create a device with the same name, site, and tenant as an existing device violates a unique constraint on the Device model, but it is caught and raised cleanly as a validation error:
The same thing should be happening for FHRP group assignments, without needing any custom code. I suspect it might have to do with a difference in the model and/or form classes used.
The error happens from https://github.com/django/django/blob/main/django/forms/models.py#L390 as the fields aren't in the form so Django skips validating the unique constraints for them, dcim ones are in the form. Several different ways to fix this:
- I looked at putting them into the form as hidden fields, but the code is also needed to set them - IMHO not obvious what the code is for doing it this way.
- Looked at overriding _get_validation_exclusions but had side-effects.
I think having it in the clean is the most obvious and direct way to fix?