# Buggy code
def __init__(self, *args, **kwargs):
    # Initialize helper selectors
    instance = kwargs.get("instance")
    initial = kwargs.get("initial", {}).copy()
    if instance:
        if type(instance.assigned_object) is Interface:
            initial["device"] = instance.assigned_object.device
            initial["interface"] = instance.assigned_object
        elif type(instance.assigned_object) is VMInterface:
            initial["virtual_machine"] = instance.assigned_object.virtual_machine
            initial["vminterface"] = instance.assigned_object
        if instance.nat_inside and instance.nat_inside.device is not None:
            initial["nat_site"] = instance.nat_inside.device.site
            initial["nat_rack"] = instance.nat_inside.device.rack
            initial["nat_device"] = instance.nat_inside.device
    kwargs["initial"] = initial

    super().__init__(*args, **kwargs)

    self.fields["vrf"].empty_label = "Global"

    # Initialize primary_for_parent if IP address is already assigned
    if self.instance.pk and self.instance.assigned_object:
        parent = self.instance.assigned_object.parent
        if (
            self.instance.address.version == 4
            and parent.primary_ip4_id == self.instance.pk
            or self.instance.address.version == 6
            and parent.primary_ip6_id == self.instance.pk
        ):
            self.initial["primary_for_parent"] = True


# Traceback (AttributeError)
Traceback (most recent call last):
File "/home/jstretch/.virtualenvs/netbox-2.9/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/jstretch/.virtualenvs/netbox-2.9/lib/python3.6/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jstretch/.virtualenvs/netbox-2.9/lib/python3.6/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 390, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/opt/netbox/netbox/utilities/views.py", line 121, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/home/jstretch/.virtualenvs/netbox-2.9/lib/python3.6/site-packages/django/views/generic/base.py", line 101, in dispatch
return handler(request, *args, **kwargs)
File "/opt/netbox/netbox/utilities/views.py", line 396, in get
form = self.model_form(instance=obj, initial=initial_data)
File "/opt/netbox/netbox/ipam/forms.py", line 619, in __init__
if instance.nat_inside and instance.nat_inside.device is not None:

Exception Type: AttributeError at /ipam/ip-addresses/56298/edit/
Exception Value: 'IPAddress' object has no attribute 'device'

# Fixed code
def __init__(self, *args, **kwargs):
    # Initialize helper selectors
    instance = kwargs.get("instance")
    initial = kwargs.get("initial", {}).copy()
    if instance:
        if type(instance.assigned_object) is Interface:
            initial["device"] = instance.assigned_object.device
            initial["interface"] = instance.assigned_object
        elif type(instance.assigned_object) is VMInterface:
            initial["virtual_machine"] = instance.assigned_object.virtual_machine
            initial["vminterface"] = instance.assigned_object
        if instance.nat_inside:
            nat_inside_parent = instance.nat_inside.assigned_object
            if type(nat_inside_parent) is Interface:
                initial["nat_site"] = nat_inside_parent.device.site.pk
                initial["nat_rack"] = nat_inside_parent.device.rack.pk
                initial["nat_device"] = nat_inside_parent.device.pk
            elif type(nat_inside_parent) is VMInterface:
                initial["nat_cluster"] = nat_inside_parent.virtual_machine.cluster.pk
                initial["nat_virtual_machine"] = nat_inside_parent.virtual_machine.pk
    kwargs["initial"] = initial

    super().__init__(*args, **kwargs)

    self.fields["vrf"].empty_label = "Global"

    # Initialize primary_for_parent if IP address is already assigned
    if self.instance.pk and self.instance.assigned_object:
        parent = self.instance.assigned_object.parent
        if (
            self.instance.address.version == 4
            and parent.primary_ip4_id == self.instance.pk
            or self.instance.address.version == 6
            and parent.primary_ip6_id == self.instance.pk
        ):
            self.initial["primary_for_parent"] = True