Make the override check a utility function

This commit is contained in:
Ryan Hiebert 2015-11-16 12:21:58 -06:00
parent fe12816b82
commit dca2de3a5c
2 changed files with 9 additions and 4 deletions

View File

@ -18,7 +18,7 @@ from rest_framework.fields import (
Field, empty, get_attribute, is_simple_callable, iter_options
)
from rest_framework.reverse import reverse
from rest_framework.utils import html
from rest_framework.utils import html, overridden
class Hyperlink(six.text_type):
@ -63,9 +63,7 @@ class RelatedField(Field):
self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff)
self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text)
default_get_queryset = getattr(RelatedField.get_queryset, '__func__',
RelatedField.get_queryset) # Python 2/3
if self.get_queryset.__func__ == default_get_queryset:
if not overridden.method_overridden('get_queryset', RelatedField, self):
assert self.queryset is not None or kwargs.get('read_only', None), (
'Relational field must provide a `queryset` argument, '
'or set read_only=`True`.'

View File

@ -0,0 +1,7 @@
def method_overridden(method_name, klass, instance):
"""
Determine if a method has been overridden.
"""
method = getattr(klass, method_name)
default_method = getattr(method, '__func__', method) # Python 3 compat
return default_method is not getattr(instance, method_name).__func__