# Buggy code
def validate(self, data):
    # Validate uniqueness of name and slug if a site has been assigned.
    if data.get("site", None):
        for field in ["name", "slug"]:
            validator = UniqueTogetherValidator(
                queryset=VLANGroup.objects.all(), fields=("site", field)
            )
            validator.set_context(self)
            validator(data)

    # Enforce model validation
    super().validate(data)

    return data


# Traceback (AttributeError)
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'

# Fixed code
def validate(self, data):
    # Validate uniqueness of name and slug if a site has been assigned.
    if data.get("site", None):
        for field in ["name", "slug"]:
            validator = UniqueTogetherValidator(
                queryset=VLANGroup.objects.all(), fields=("site", field)
            )
            validator(data, self)

    # Enforce model validation
    super().validate(data)

    return data