diff options
| author | Tom Christie | 2014-09-24 14:09:49 +0100 |
|---|---|---|
| committer | Tom Christie | 2014-09-24 14:09:49 +0100 |
| commit | f4b1dcb167be0bbdaae2cc2a92f651536896dc16 (patch) | |
| tree | 855df19d2a09da4329274db4bc56d89b342df036 /rest_framework/utils | |
| parent | aa84432f9b40849fb677d9fed803098fd392f881 (diff) | |
| download | django-rest-framework-f4b1dcb167be0bbdaae2cc2a92f651536896dc16.tar.bz2 | |
OPTIONS support
Diffstat (limited to 'rest_framework/utils')
| -rw-r--r-- | rest_framework/utils/field_mapping.py | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py index c208afdc..c3794083 100644 --- a/rest_framework/utils/field_mapping.py +++ b/rest_framework/utils/field_mapping.py @@ -9,17 +9,21 @@ from rest_framework.compat import clean_manytomany_helptext import inspect -def lookup_class(mapping, instance): +class ClassLookupDict(object): """ - Takes a dictionary with classes as keys, and an object. - Traverses the object's inheritance hierarchy in method - resolution order, and returns the first matching value + Takes a dictionary with classes as keys. + Lookups against this object will traverses the object's inheritance + hierarchy in method resolution order, and returns the first matching value from the dictionary or raises a KeyError if nothing matches. """ - for cls in inspect.getmro(instance.__class__): - if cls in mapping: - return mapping[cls] - raise KeyError('Class %s not found in lookup.', cls.__name__) + def __init__(self, mapping): + self.mapping = mapping + + def __getitem__(self, key): + for cls in inspect.getmro(key.__class__): + if cls in self.mapping: + return self.mapping[cls] + raise KeyError('Class %s not found in lookup.', cls.__name__) def needs_label(model_field, field_name): |
