This commit is contained in:
Alireza Savand 2016-10-10 06:31:03 +00:00 committed by GitHub
commit f9d3fecabd
2 changed files with 15 additions and 3 deletions

View File

@ -140,6 +140,16 @@ def value_from_object(field, obj):
return field.value_from_object(obj)
def getargspec(obj):
if not hasattr(inspect, 'signature'): # Python 2.7 - 3.2
parameters, _, _, defaults = inspect.getargspec(obj)
else: # Python +3.3
signature = inspect.signature(obj)
parameters = signature.parameters
defaults = [i for i in parameters if i.default != inspect._empty]
return parameters, defaults
# contrib.postgres only supported from 1.8 onwards.
try:
from django.contrib.postgres import fields as postgres_fields

View File

@ -32,7 +32,8 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import ISO_8601
from rest_framework.compat import (
get_remote_field, unicode_repr, unicode_to_repr, value_from_object
get_remote_field, getargspec, unicode_repr, unicode_to_repr,
value_from_object
)
from rest_framework.exceptions import ValidationError
from rest_framework.settings import api_settings
@ -59,8 +60,9 @@ def is_simple_callable(obj):
if not (function or method):
return False
args, _, _, defaults = inspect.getargspec(obj)
len_args = len(args) if function else len(args) - 1
parameters, defaults = getargspec(obj)
len_args = len(parameters) if function else len(parameters) - 1
len_defaults = len(defaults) if defaults else 0
return len_args <= len_defaults