# Buggy code
def to_internal_value(self, data):
    # TODO: remove when API v1 is removed
    if "credential_type" not in data and self.version == 1:
        # If `credential_type` is not provided, assume the payload is a
        # v1 credential payload that specifies a `kind` and a flat list
        # of field values
        #
        # In this scenario, we should automatically detect the proper
        # CredentialType based on the provided values
        kind = data.get("kind", "ssh")
        credential_type = CredentialType.from_v1_kind(kind, data)
        if credential_type is None:
            raise serializers.ValidationError(
                {"kind": _('"%s" is not a valid choice' % kind)}
            )
        data["credential_type"] = credential_type.pk
        value = OrderedDict(
            {"credential_type": credential_type}.items()
            + super(CredentialSerializer, self).to_internal_value(data).items()
        )

        # Make a set of the keys in the POST/PUT payload
        # - Subtract real fields (name, organization, inputs)
        # - Subtract virtual v1 fields defined on the determined credential
        #   type (username, password, etc...)
        # - Any leftovers are invalid for the determined credential type
        valid_fields = set(super(CredentialSerializer, self).get_fields().keys())
        valid_fields.update(V2CredentialFields().get_fields().keys())
        valid_fields.update(["kind", "cloud"])

        for field in (
            set(data.keys()) - valid_fields - set(credential_type.defined_fields)
        ):
            if data.get(field):
                raise serializers.ValidationError(
                    {
                        "detail": _("'%s' is not a valid field for %s")
                        % (field, credential_type.name)
                    }
                )
        value.pop("kind", None)
        return value
    return super(CredentialSerializer, self).to_internal_value(data)


# Traceback (AttributeError)
AttributeError: 'super' object has no attribute 'accessible_pk_qs'
2018-03-13 19:49:42,026 ERROR    django.request Internal Server Error: /api/v2/inventory_sources/
Traceback (most recent call last):
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/lib/python2.7/site-packages/awx/wsgi.py", line 65, in _legacy_get_response
return super(AWXWSGIHandler, self)._legacy_get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/awx/api/generics.py", line 284, in dispatch
return super(APIView, self).dispatch(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/views.py", line 494, in dispatch
response = self.handle_exception(exc)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/views.py", line 454, in handle_exception
self.raise_uncaught_exception(exc)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/views.py", line 491, in dispatch
response = handler(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/generics.py", line 201, in get
return self.list(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/mixins.py", line 45, in list
return self.get_paginated_response(serializer.data)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/serializers.py", line 742, in data
ret = super(ListSerializer, self).data
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/serializers.py", line 660, in to_representation
self.child.to_representation(item) for item in iterable
File "/usr/lib/python2.7/site-packages/awx/api/serializers.py", line 2070, in to_representation
ret = super(InventorySourceSerializer, self).to_representation(obj)
File "/usr/lib/python2.7/site-packages/awx/api/serializers.py", line 683, in to_representation
return super(UnifiedJobTemplateSerializer, self).to_representation(obj)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/serializers.py", line 504, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/var/lib/awx/venv/awx/lib/python2.7/site-packages/rest_framework/fields.py", line 1816, in to_representation
return method(value)
File "/usr/lib/python2.7/site-packages/awx/api/serializers.py", line 346, in _get_summary_fields
return {} if obj is None else self.get_summary_fields(obj)
File "/usr/lib/python2.7/site-packages/awx/api/serializers.py", line 2038, in get_summary_fields
summary_fields = super(InventorySourceSerializer, self).get_summary_fields(obj)
File "/usr/lib/python2.7/site-packages/awx/api/serializers.py", line 1973, in get_summary_fields
summary_fields = super(InventorySourceOptionsSerializer, self).get_summary_fields(obj)
File "/usr/lib/python2.7/site-packages/awx/api/serializers.py", line 407, in get_summary_fields
user_capabilities = self._obj_capability_dict(obj)
File "/usr/lib/python2.7/site-packages/awx/api/serializers.py", line 436, in _obj_capability_dict
model, qs, prefetch_list, view.request.user
File "/usr/lib/python2.7/site-packages/awx/main/utils/common.py", line 571, in prefetch_page_capabilities
filter_args.append(Q(**{'pk__in': model.accessible_pk_qs(user, '%s_role' % role_type)}))
File "/usr/lib/python2.7/site-packages/awx/main/models/unified_jobs.py", line 199, in accessible_pk_qs
return super(UnifiedJobTemplate, cls).accessible_pk_qs(accessor, role_field)
AttributeError: 'super' object has no attribute 'accessible_pk_qs'

# Fixed code
def to_internal_value(self, data):
    # TODO: remove when API v1 is removed
    if "credential_type" not in data and self.version == 1:
        # If `credential_type` is not provided, assume the payload is a
        # v1 credential payload that specifies a `kind` and a flat list
        # of field values
        #
        # In this scenario, we should automatically detect the proper
        # CredentialType based on the provided values
        kind = data.get("kind", "ssh")
        credential_type = CredentialType.from_v1_kind(kind, data)
        if credential_type is None:
            raise serializers.ValidationError(
                {"kind": _('"%s" is not a valid choice' % kind)}
            )
        data["credential_type"] = credential_type.pk
        value = OrderedDict(
            {"credential_type": credential_type}.items()
            + super(CredentialSerializer, self).to_internal_value(data).items()
        )

        # Make a set of the keys in the POST/PUT payload
        # - Subtract real fields (name, organization, inputs)
        # - Subtract virtual v1 fields defined on the determined credential
        #   type (username, password, etc...)
        # - Any leftovers are invalid for the determined credential type
        valid_fields = set(super(CredentialSerializer, self).get_fields().keys())
        valid_fields.update(V2CredentialFields().get_fields().keys())
        valid_fields.update(["kind", "cloud"])

        for field in (
            set(data.keys()) - valid_fields - set(credential_type.defined_fields)
        ):
            if data.get(field):
                raise serializers.ValidationError(
                    {
                        "detail": _(
                            "'{field_name}' is not a valid field for {credential_type_name}"
                        ).format(
                            field_name=field, credential_type_name=credential_type.name
                        )
                    }
                )
        value.pop("kind", None)
        return value
    return super(CredentialSerializer, self).to_internal_value(data)