From 792bc4d60814af026f926a4c92a9d4a8e3124341 Mon Sep 17 00:00:00 2001 From: Sébastien Piquemal Date: Tue, 10 Jan 2012 20:38:01 +0200 Subject: fixed issue#73 and added a test --- djangorestframework/serializer.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'djangorestframework/serializer.py') diff --git a/djangorestframework/serializer.py b/djangorestframework/serializer.py index 4e1c6b80..43d32b29 100644 --- a/djangorestframework/serializer.py +++ b/djangorestframework/serializer.py @@ -230,12 +230,14 @@ class Serializer(object): # serialize each required field for fname in fields: try: - if inspect.ismethod(getattr(self, fname, None)) and \ - len(inspect.getargspec(getattr(self, fname))[0]) == 2: - # check first for a method 'fname' on self first + # we first check for a method 'fname' on self, + # 'fname's signature must be 'def fname(self, instance)' + meth = getattr(self, fname, None) + if (inspect.ismethod(meth) and + len(inspect.getargspec(meth)[0]) == 2): obj = meth(instance) elif hasattr(instance, '__contains__') and fname in instance: - # check for a key 'fname' on the instance + # then check for a key 'fname' on the instance obj = instance[fname] elif hasattr(instance, smart_str(fname)): # finally check for an attribute 'fname' on the instance -- cgit v1.2.3