Commit 31a73beb authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

Extend model_to_dict utility to include object

properties and obj string representation
parent 3511df6f
...@@ -218,6 +218,9 @@ def get_query(request): ...@@ -218,6 +218,9 @@ def get_query(request):
except AttributeError: except AttributeError:
return {} return {}
def get_properties(obj):
return (i for i in vars(obj.__class__) \
if isinstance(getattr(obj.__class__, i), property))
def model_to_dict(obj, exclude=['AutoField', 'ForeignKey', 'OneToOneField'], def model_to_dict(obj, exclude=['AutoField', 'ForeignKey', 'OneToOneField'],
include_empty=True): include_empty=True):
...@@ -265,6 +268,10 @@ def model_to_dict(obj, exclude=['AutoField', 'ForeignKey', 'OneToOneField'], ...@@ -265,6 +268,10 @@ def model_to_dict(obj, exclude=['AutoField', 'ForeignKey', 'OneToOneField'],
value = unicode(value) if value is not None else value value = unicode(value) if value is not None else value
if value or include_empty: if value or include_empty:
tree[field_name] = value tree[field_name] = value
properties = list(get_properties(obj))
for p in properties:
tree[p] = getattr(obj, p)
tree['str_repr'] = obj.__str__()
return tree return tree
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment