Unable to import child devices where parent name is duplicated at different sites #4036

Closed
opened 2025-12-29 18:32:44 +01:00 by adam · 1 comment
Owner

Originally created by @IdRatherStand on GitHub (Aug 27, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.6.8
  • NetBox version: v2.8.7

Imports currently fail when a child device import is attempted using a Parent device name that is not unique. I suspect this issue will also apply to Cables and any other imports that reference a device using simply the name.

It looks like imports of certain types need to have the "site" field made available to them, as otherwise when results > 1 I presume the import will stop (as the below example does).

I can see Issue #2669 which refers to a change in Netbox allowing duplicate names, and in the thread there is talk of further fields but not sure if this was implemented, or in scope. If this is not a supported function, and the solution is to uniquely name devices then this should probably be validated on import, or users made aware.

Steps to Reproduce

  1. Create a site: Test Site A

  2. Create a rack: Test Rack A
    Site: Test Site A
    Role: Anything

  3. Create a site: Test Site B

  4. Create a rack: Test Rack B
    Site: Test Site B
    Role: Anything

  5. Create a devicetype
    Name: Parent Test Device
    Manufacturer: Test Manufacturer
    Parent / Child: Parent
    U: 1
    Device Bay: 1 (Named '1')

  6. Create a devicetype
    Name: Child Test Device
    Manufacturer: Test Manufacturer
    Parent / Child: Child
    U: 0

  7. Create a device
    Name: Parent Device
    Role: Legacy Switch
    Manufacturer: Test Manufacturer
    Type: Parent Test Device
    Site: Test Site A
    Rack: Test Rack A

  8. Create another device (note the different site)
    Name: Parent Device
    Role: Legacy Switch
    Manufacturer: Test Manufacturer
    Type: Parent Test Device
    Site: Test Site B
    Rack: Test Rack B

  9. Attempt to import a child device:
    name,device_role,manufacturer,device_type,status,parent,device_bay
    Child Device,Legacy Switch,Test Manufacturer,Child Test Device,Active,Parent Device,1

Traceback follows:

Environment:


Request Method: POST
Request URL: https://xxx/dcim/devices/import/child-devices/

Django Version: 3.0.8
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'cacheops',
 'corsheaders',
 'debug_toolbar',
 'django_filters',
 'django_tables2',
 'django_prometheus',
 'mptt',
 'rest_framework',
 'taggit',
 'taggit_serializer',
 'timezone_field',
 'circuits',
 'dcim',
 'ipam',
 'extras',
 'secrets',
 'tenancy',
 'users',
 'utilities',
 'virtualization',
 'django_rq',
 'drf_yasg']
Installed Middleware:
['debug_toolbar.middleware.DebugToolbarMiddleware',
 'django_prometheus.middleware.PrometheusBeforeMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'utilities.middleware.ExceptionHandlingMiddleware',
 'utilities.middleware.RemoteUserMiddleware',
 'utilities.middleware.LoginRequiredMiddleware',
 'utilities.middleware.APIVersionMiddleware',
 'extras.middleware.ObjectChangeMiddleware',
 'django_prometheus.middleware.PrometheusAfterMiddleware']



Traceback (most recent call last):
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/contrib/auth/mixins.py", line 85, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/views/generic/base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "/opt/netbox/netbox/utilities/views.py", line 622, in post
    if obj_form.is_valid():
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/forms.py", line 180, in is_valid
    return self.is_bound and not self.errors
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/forms.py", line 175, in errors
    self.full_clean()
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/forms.py", line 378, in full_clean
    self._post_clean()
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/models.py", line 410, in _post_clean
    self.validate_unique()
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/models.py", line 419, in validate_unique
    self.instance.validate_unique(exclude=exclude)
  File "/opt/netbox/netbox/dcim/models/__init__.py", line 1474, in validate_unique
    if Device.objects.exclude(pk=self.pk).filter(name=self.name, site=self.site, tenant__isnull=True):
  File "/opt/netbox/venv/lib64/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 197, in __get__
    "%s has no %s." % (self.field.model.__name__, self.field.name)

Exception Type: RelatedObjectDoesNotExist at /dcim/devices/import/child-devices/
Exception Value: Device has no site.

What did you expect to happen?

Child device imported into Netbox (cannot see how this is possible without specifying the site, however when doing this the validation states that site is not a valid field)

What happened instead?

Error stating "Device has no site."

Originally created by @IdRatherStand on GitHub (Aug 27, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.6.8 * NetBox version: v2.8.7 Imports currently fail when a child device import is attempted using a Parent device name that is not unique. I suspect this issue will also apply to Cables and any other imports that reference a device using simply the name. It looks like imports of certain types need to have the "site" field made available to them, as otherwise when results > 1 I presume the import will stop (as the below example does). I can see Issue #2669 which refers to a change in Netbox allowing duplicate names, and in the thread there is talk of further fields but not sure if this was implemented, or in scope. If this is not a supported function, and the solution is to uniquely name devices then this should probably be validated on import, or users made aware. ### Steps to Reproduce 1) Create a site: Test Site A 2) Create a rack: Test Rack A Site: Test Site A Role: Anything 3) Create a site: Test Site B 4) Create a rack: Test Rack B Site: Test Site B Role: Anything 5) Create a devicetype Name: Parent Test Device Manufacturer: Test Manufacturer Parent / Child: Parent U: 1 Device Bay: 1 (Named '1') 6) Create a devicetype Name: Child Test Device Manufacturer: Test Manufacturer Parent / Child: Child U: 0 7) Create a device Name: Parent Device Role: Legacy Switch Manufacturer: Test Manufacturer Type: Parent Test Device Site: Test Site A Rack: Test Rack A 8) Create another device (note the different site) Name: Parent Device Role: Legacy Switch Manufacturer: Test Manufacturer Type: Parent Test Device Site: Test Site B Rack: Test Rack B 9) Attempt to import a child device: name,device_role,manufacturer,device_type,status,parent,device_bay Child Device,Legacy Switch,Test Manufacturer,Child Test Device,Active,Parent Device,1 Traceback follows: ``` Environment: Request Method: POST Request URL: https://xxx/dcim/devices/import/child-devices/ Django Version: 3.0.8 Python Version: 3.6.8 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize', 'cacheops', 'corsheaders', 'debug_toolbar', 'django_filters', 'django_tables2', 'django_prometheus', 'mptt', 'rest_framework', 'taggit', 'taggit_serializer', 'timezone_field', 'circuits', 'dcim', 'ipam', 'extras', 'secrets', 'tenancy', 'users', 'utilities', 'virtualization', 'django_rq', 'drf_yasg'] Installed Middleware: ['debug_toolbar.middleware.DebugToolbarMiddleware', 'django_prometheus.middleware.PrometheusBeforeMiddleware', 'corsheaders.middleware.CorsMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware', 'utilities.middleware.ExceptionHandlingMiddleware', 'utilities.middleware.RemoteUserMiddleware', 'utilities.middleware.LoginRequiredMiddleware', 'utilities.middleware.APIVersionMiddleware', 'extras.middleware.ObjectChangeMiddleware', 'django_prometheus.middleware.PrometheusAfterMiddleware'] Traceback (most recent call last): File "/opt/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/opt/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/opt/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/netbox/venv/lib64/python3.6/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, *args, **kwargs) File "/opt/netbox/venv/lib64/python3.6/site-packages/django/contrib/auth/mixins.py", line 85, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/venv/lib64/python3.6/site-packages/django/views/generic/base.py", line 97, in dispatch return handler(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/views.py", line 622, in post if obj_form.is_valid(): File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/forms.py", line 180, in is_valid return self.is_bound and not self.errors File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/forms.py", line 175, in errors self.full_clean() File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/forms.py", line 378, in full_clean self._post_clean() File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/models.py", line 410, in _post_clean self.validate_unique() File "/opt/netbox/venv/lib64/python3.6/site-packages/django/forms/models.py", line 419, in validate_unique self.instance.validate_unique(exclude=exclude) File "/opt/netbox/netbox/dcim/models/__init__.py", line 1474, in validate_unique if Device.objects.exclude(pk=self.pk).filter(name=self.name, site=self.site, tenant__isnull=True): File "/opt/netbox/venv/lib64/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 197, in __get__ "%s has no %s." % (self.field.model.__name__, self.field.name) Exception Type: RelatedObjectDoesNotExist at /dcim/devices/import/child-devices/ Exception Value: Device has no site. ``` ### What did you expect to happen? Child device imported into Netbox (cannot see how this is possible without specifying the site, however when doing this the validation states that site is not a valid field) ### What happened instead? Error stating "Device has no site."
adam added the type: bugstatus: accepted labels 2025-12-29 18:32:44 +01:00
adam closed this issue 2025-12-29 18:32:44 +01:00
Author
Owner

@jeremystretch commented on GitHub (Aug 27, 2020):

I've resolved the underlying bug causing this issue in 5cdccb4. To import a child device when its parent has the same name as another device, you can use an alternate identifier; e.g. parent.id to specify the parent device by its unique ID.

@jeremystretch commented on GitHub (Aug 27, 2020): I've resolved the underlying bug causing this issue in 5cdccb4. To import a child device when its parent has the same name as another device, you can use an alternate identifier; e.g. `parent.id` to specify the parent device by its unique ID.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4036