# Buggy code
def clean(self):
    if self.address:
        # /0 masks are not acceptable
        if self.address.prefixlen == 0:
            raise ValidationError({"address": "Cannot create IP address with /0 mask."})

        # Enforce unique IP space (if applicable)
        if self.role not in IPADDRESS_ROLES_NONUNIQUE and (
            (self.vrf is None and settings.ENFORCE_GLOBAL_UNIQUE)
            or (self.vrf and self.vrf.enforce_unique)
        ):
            duplicate_ips = self.get_duplicates()
            if duplicate_ips:
                raise ValidationError(
                    {
                        "address": "Duplicate IP address found in {}: {}".format(
                            "VRF {}".format(self.vrf) if self.vrf else "global table",
                            duplicate_ips.first(),
                        )
                    }
                )

    # Check for primary IP assignment that doesn't match the assigned device/VM
    if self.pk and type(self.assigned_object) is Interface:
        device = Device.objects.filter(
            Q(primary_ip4=self) | Q(primary_ip6=self)
        ).first()
        if device:
            if self.assigned_object is None:
                raise ValidationError(
                    {
                        "interface": f"IP address is primary for device {device} but not assigned to an interface"
                    }
                )
            elif self.assigned_object.device != device:
                raise ValidationError(
                    {
                        "interface": f"IP address is primary for device {device} but assigned to "
                        f"{self.assigned_object.device} ({self.assigned_object})"
                    }
                )
    elif self.pk and type(self.assigned_object) is VMInterface:
        vm = VirtualMachine.objects.filter(
            Q(primary_ip4=self) | Q(primary_ip6=self)
        ).first()
        if vm:
            if self.assigned_object is None:
                raise ValidationError(
                    {
                        "vminterface": f"IP address is primary for virtual machine {vm} but not assigned to an "
                        f"interface"
                    }
                )
            elif self.interface.virtual_machine != vm:
                raise ValidationError(
                    {
                        "vminterface": f"IP address is primary for virtual machine {vm} but assigned to "
                        f"{self.assigned_object.virtual_machine} ({self.assigned_object})"
                    }
                )


# Traceback (AttributeError)
Traceback (most recent call last):
File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 73, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/netbox/netbox/utilities/views.py", line 392, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/opt/netbox/netbox/utilities/views.py", line 123, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 101, in dispatch
return handler(request, *args, **kwargs)
File "/opt/netbox/netbox/utilities/views.py", line 418, in post
if form.is_valid():
File "/opt/netbox/venv/lib/python3.7/site-packages/django/forms/forms.py", line 177, in is_valid
return self.is_bound and not self.errors
File "/opt/netbox/venv/lib/python3.7/site-packages/django/forms/forms.py", line 172, in errors
self.full_clean()
File "/opt/netbox/venv/lib/python3.7/site-packages/django/forms/forms.py", line 376, in full_clean
self._post_clean()
File "/opt/netbox/venv/lib/python3.7/site-packages/django/forms/models.py", line 405, in _post_clean
self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/opt/netbox/venv/lib/python3.7/site-packages/django/db/models/base.py", line 1213, in full_clean
self.clean()
File "/opt/netbox/netbox/ipam/models.py", line 748, in clean
elif self.interface.virtual_machine != vm:

Exception Type: AttributeError at /ipam/ip-addresses/4195/edit/
Exception Value: 'RelatedManager' object has no attribute 'virtual_machine'

# Fixed code
def clean(self):
    if self.address:
        # /0 masks are not acceptable
        if self.address.prefixlen == 0:
            raise ValidationError({"address": "Cannot create IP address with /0 mask."})

        # Enforce unique IP space (if applicable)
        if self.role not in IPADDRESS_ROLES_NONUNIQUE and (
            (self.vrf is None and settings.ENFORCE_GLOBAL_UNIQUE)
            or (self.vrf and self.vrf.enforce_unique)
        ):
            duplicate_ips = self.get_duplicates()
            if duplicate_ips:
                raise ValidationError(
                    {
                        "address": "Duplicate IP address found in {}: {}".format(
                            "VRF {}".format(self.vrf) if self.vrf else "global table",
                            duplicate_ips.first(),
                        )
                    }
                )

    # Check for primary IP assignment that doesn't match the assigned device/VM
    if self.pk and type(self.assigned_object) is Interface:
        device = Device.objects.filter(
            Q(primary_ip4=self) | Q(primary_ip6=self)
        ).first()
        if device:
            if self.assigned_object is None:
                raise ValidationError(
                    {
                        "interface": f"IP address is primary for device {device} but not assigned to an interface"
                    }
                )
            elif self.assigned_object.device != device:
                raise ValidationError(
                    {
                        "interface": f"IP address is primary for device {device} but assigned to "
                        f"{self.assigned_object.device} ({self.assigned_object})"
                    }
                )
    elif self.pk and type(self.assigned_object) is VMInterface:
        vm = VirtualMachine.objects.filter(
            Q(primary_ip4=self) | Q(primary_ip6=self)
        ).first()
        if vm:
            if self.assigned_object is None:
                raise ValidationError(
                    {
                        "vminterface": f"IP address is primary for virtual machine {vm} but not assigned to an "
                        f"interface"
                    }
                )
            elif self.assigned_object.virtual_machine != vm:
                raise ValidationError(
                    {
                        "vminterface": f"IP address is primary for virtual machine {vm} but assigned to "
                        f"{self.assigned_object.virtual_machine} ({self.assigned_object})"
                    }
                )