Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Links2004 opened this issue Apr 15, 2020 · 4 comments
Assignees
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@Links2004
Copy link

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'
@Links2004
Copy link
Author

Links2004 commented Apr 15, 2020

this may is related to
encode/django-rest-framework#7062
and
encode/django-rest-framework#6172

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

@Links2004 Links2004 changed the title AttributeError at /api/ipam/vlans/ 'UniqueTogetherValidator' object has no attribute 'set_context' django-rest-framework v3.11 dropped suppport for set_context in 'UniqueTogetherValidator' Apr 15, 2020
@Links2004 Links2004 changed the title django-rest-framework v3.11 dropped suppport for set_context in 'UniqueTogetherValidator' django-rest-framework v3.11 dropped suppport for set_context in UniqueTogetherValidator Apr 15, 2020
@DouglasHeriot
Copy link

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 613e378

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

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application labels Apr 15, 2020
@jeremystretch jeremystretch self-assigned this Apr 15, 2020
@jeremystretch
Copy link
Member

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

@kr3ator
Copy link

kr3ator commented Apr 21, 2020

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

DouglasHeriot added a commit to hillsong/ansible_modules that referenced this issue Apr 21, 2020
DouglasHeriot added a commit to hillsong/ansible_modules that referenced this issue Apr 21, 2020
Until netbox-community/netbox#4496 is released

Also reduced verbosity of integration tests - there's a lot of output.
DouglasHeriot added a commit to hillsong/ansible_modules that referenced this issue Apr 21, 2020
DouglasHeriot added a commit to hillsong/ansible_modules that referenced this issue Apr 27, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

4 participants