django-rest-framework v3.11 dropped suppport for set_context in UniqueTogetherValidator #3575

Closed
opened 2025-12-29 18:29:58 +01:00 by adam · 4 comments
Owner

Originally created by @Links2004 on GitHub (Apr 15, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.7.3
  • NetBox version: 2.8.0 and master

When we try to create a vlan via the RESTapi the result is 500 when we try to set the group,
we tested this with v2.8.0 and master branch,
before the update from v2.7.8 this where working fine.

Steps to Reproduce

  1. Disable any installed plugins by commenting out the PLUGINS setting in
    configuration.py.
  2. create site KAR-DC
  3. create VLAN groups
    GET "api/ipam/vlan-groups/"
{
  "count": 2,
  "next": null,
  "previous": null,
  "results": [
    {
      "id": 1,
      "name": "C01",
      "slug": "c01",
      "site": {
        "id": 1,
        "url": "http://xxx/api/dcim/sites/1/",
        "name": "KAR-DC",
        "slug": "kar-dc"
      },
      "description": "",
      "vlan_count": 13
    },
    {
      "id": 2,
      "name": "QA",
      "slug": "qa",
      "site": {
        "id": 1,
        "url": "http://xxx/api/dcim/sites/1/",
        "name": "KAR-DC",
        "slug": "kar-dc"
      },
      "description": "",
      "vlan_count": 2
    }
  ]
}
  1. send POST
curl -X POST "http://xxx/api/ipam/vlans/" -H "accept: application/json" -H "Content-Type: application/json" -H "X-CSRFToken: " -d "{ \"vid\": 3098, \"name\": \"sgu-8-lan\", \"group\": { \"slug\": \"c01\" }}"

Expected Behavior

VLAN is created

Observed Behavior

error:

AttributeError at /api/ipam/vlans/
'UniqueTogetherValidator' object has no attribute 'set_context'

Request Method: POST
Request URL: http://10.0.192.20/api/ipam/vlans/
Django Version: 3.0.5
Python Executable: /opt/netbox/venv/bin/python3
Python Version: 3.7.3
Python Path: ['/opt/netbox/venv/bin', '/opt/netbox/netbox', '/', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/opt/netbox/venv/lib/python3.7/site-packages']
Server time: Wed, 15 Apr 2020 10:01:42 +0000
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/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/viewsets.py", line 114, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/netbox/netbox/utilities/api.py", line 330, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch
    response = self.handle_exception(exc)
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
    raise exc
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch
    response = handler(request, *args, **kwargs)
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/mixins.py", line 18, in create
    serializer.is_valid(raise_exception=True)
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 234, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 436, in run_validation
    value = self.validate(value)
  File "/opt/netbox/netbox/ipam/api/serializers.py", line 125, in validate
    validator.set_context(self)

Exception Type: AttributeError at /api/ipam/vlans/
Exception Value: 'UniqueTogetherValidator' object has no attribute 'set_context'
Originally created by @Links2004 on GitHub (Apr 15, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.7.3 * NetBox version: 2.8.0 and master When we try to create a vlan via the RESTapi the result is 500 when we try to set the `group`, we tested this with v2.8.0 and master branch, before the update from v2.7.8 this where working fine. ### Steps to Reproduce 1. Disable any installed plugins by commenting out the `PLUGINS` setting in `configuration.py`. 2. create site `KAR-DC` 3. create VLAN groups GET "api/ipam/vlan-groups/" ``` { "count": 2, "next": null, "previous": null, "results": [ { "id": 1, "name": "C01", "slug": "c01", "site": { "id": 1, "url": "http://xxx/api/dcim/sites/1/", "name": "KAR-DC", "slug": "kar-dc" }, "description": "", "vlan_count": 13 }, { "id": 2, "name": "QA", "slug": "qa", "site": { "id": 1, "url": "http://xxx/api/dcim/sites/1/", "name": "KAR-DC", "slug": "kar-dc" }, "description": "", "vlan_count": 2 } ] } ``` 4. send POST ``` curl -X POST "http://xxx/api/ipam/vlans/" -H "accept: application/json" -H "Content-Type: application/json" -H "X-CSRFToken: " -d "{ \"vid\": 3098, \"name\": \"sgu-8-lan\", \"group\": { \"slug\": \"c01\" }}" ``` ### Expected Behavior VLAN is created ### Observed Behavior error: ``` AttributeError at /api/ipam/vlans/ 'UniqueTogetherValidator' object has no attribute 'set_context' Request Method: POST Request URL: http://10.0.192.20/api/ipam/vlans/ Django Version: 3.0.5 Python Executable: /opt/netbox/venv/bin/python3 Python Version: 3.7.3 Python Path: ['/opt/netbox/venv/bin', '/opt/netbox/netbox', '/', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/opt/netbox/venv/lib/python3.7/site-packages'] Server time: Wed, 15 Apr 2020 10:01:42 +0000 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/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/opt/netbox/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/viewsets.py", line 114, in view return self.dispatch(request, *args, **kwargs) File "/opt/netbox/netbox/utilities/api.py", line 330, in dispatch return super().dispatch(request, *args, **kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 505, in dispatch response = self.handle_exception(exc) File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 465, in handle_exception self.raise_uncaught_exception(exc) File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception raise exc File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/views.py", line 502, in dispatch response = handler(request, *args, **kwargs) File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/mixins.py", line 18, in create serializer.is_valid(raise_exception=True) File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 234, in is_valid self._validated_data = self.run_validation(self.initial_data) File "/opt/netbox/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 436, in run_validation value = self.validate(value) File "/opt/netbox/netbox/ipam/api/serializers.py", line 125, in validate validator.set_context(self) Exception Type: AttributeError at /api/ipam/vlans/ Exception Value: 'UniqueTogetherValidator' object has no attribute 'set_context' ```
adam added the type: bugstatus: accepted labels 2025-12-29 18:29:58 +01:00
adam closed this issue 2025-12-29 18:29:58 +01:00
Author
Owner

@Links2004 commented on GitHub (Apr 15, 2020):

this may is related to
https://github.com/encode/django-rest-framework/pull/7062
and
https://github.com/encode/django-rest-framework/pull/6172

set_context where dropped in the django-rest-framework with version 3.11

@Links2004 commented on GitHub (Apr 15, 2020): this may is related to https://github.com/encode/django-rest-framework/pull/7062 and https://github.com/encode/django-rest-framework/pull/6172 `set_context` where dropped in the django-rest-framework with version 3.11
Author
Owner

@DouglasHeriot commented on GitHub (Apr 15, 2020):

I've just run into this as well, in the automated tests on netbox-community/ansible_modules#155

I can see this change was made in 613e37837b

The Django REST release notes seem to say this was simply deprecated and not removed, but the pull requests linked above seem otherwise.
https://www.django-rest-framework.org/community/3.11-announcement/#validator-default-context

@DouglasHeriot commented on GitHub (Apr 15, 2020): I've just run into this as well, in the automated tests on netbox-community/ansible_modules#155 I can see this change was made in 613e37837b303777b864e3e9f9781e62ffa044ca The Django REST release notes seem to say this was simply deprecated and not removed, but the pull requests linked above seem otherwise. https://www.django-rest-framework.org/community/3.11-announcement/#validator-default-context
Author
Owner

@jeremystretch commented on GitHub (Apr 15, 2020):

The root change in DRF 3.11 was addressed in a914a7c438, however a handful of serializers with their own unique calls to set_context() were missed.

@jeremystretch commented on GitHub (Apr 15, 2020): The root change in DRF 3.11 was addressed in a914a7c4382349bceca43becc9895c6cccade313, however a handful of serializers with their own unique calls to `set_context()` were missed.
Author
Owner

@kr3ator commented on GitHub (Apr 21, 2020):

When will this fix be available in the master and netbox-docker?

@kr3ator commented on GitHub (Apr 21, 2020): When will this fix be available in the master and netbox-docker?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3575