# Buggy code
def to_internal_value(self, pk):
    try:
        Credential.objects.get(pk=pk)
    except ObjectDoesNotExist:
        raise serializers.ValidationError(_("Credential {} does not exist").format(pk))
    return pk


# Traceback (TypeError)
2019-03-01 13:12:09,346 ERROR    django.request Internal Server Error: /api/v2/job_templates/
Traceback (most recent call last):
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/wsgi.py", line 71, in _legacy_get_response
return super(AWXWSGIHandler, self)._legacy_get_response(request)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/var/lib/awx/venv/awx/lib64/python3.6/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/lib64/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib64/python3.6/contextlib.py", line 52, in inner
return func(*args, **kwds)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/api/generics.py", line 324, in dispatch
return super(APIView, self).dispatch(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/views.py", line 494, in dispatch
response = self.handle_exception(exc)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/views.py", line 454, in handle_exception
self.raise_uncaught_exception(exc)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/views.py", line 491, in dispatch
response = handler(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/api/views/__init__.py", line 2195, in post
ret = super(JobTemplateList, self).post(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/generics.py", line 244, in post
return self.create(request, *args, **kwargs)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/mixins.py", line 20, in create
serializer.is_valid(raise_exception=True)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/serializers.py", line 236, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/awx/api/serializers.py", line 552, in run_validation
return super(BaseSerializer, self).run_validation(data)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/serializers.py", line 435, in run_validation
value = self.to_internal_value(data)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/serializers.py", line 465, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/fields.py", line 524, in run_validation
self.run_validators(value)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/rest_framework/fields.py", line 538, in run_validators
validator(value)
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/validators.py", line 325, in __call__
if self.compare(cleaned, self.limit_value):
File "/var/lib/awx/venv/awx/lib64/python3.6/site-packages/django/core/validators.py", line 358, in compare
return a < b
TypeError: '<' not supported between instances of 'str' and 'int'

# Fixed code
def to_internal_value(self, pk):
    try:
        pk = int(pk)
    except ValueError:
        self.fail("invalid")
    try:
        Credential.objects.get(pk=pk)
    except ObjectDoesNotExist:
        raise serializers.ValidationError(_("Credential {} does not exist").format(pk))
    return pk