mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 13:14:30 +03:00
Added possibility to specify what attributes of ForeignKeys and OneToOne fields to include via nested tuples
This commit is contained in:
parent
c7c1408875
commit
253663aee8
|
@ -62,6 +62,12 @@ def _model_to_dict(instance, resource=None):
|
||||||
|
|
||||||
# Method fields
|
# Method fields
|
||||||
for fname in extra_fields:
|
for fname in extra_fields:
|
||||||
|
|
||||||
|
if isinstance(fname, (tuple, list)):
|
||||||
|
fname, fields = fname
|
||||||
|
else:
|
||||||
|
fname, fields = fname, False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if hasattr(resource, fname):
|
if hasattr(resource, fname):
|
||||||
# check the resource first, to allow it to override fields
|
# check the resource first, to allow it to override fields
|
||||||
|
@ -77,6 +83,12 @@ def _model_to_dict(instance, resource=None):
|
||||||
|
|
||||||
# TODO: It would be nicer if this didn't recurse here.
|
# TODO: It would be nicer if this didn't recurse here.
|
||||||
# Let's keep _model_to_dict flat, and _object_to_data recursive.
|
# Let's keep _model_to_dict flat, and _object_to_data recursive.
|
||||||
|
if fields:
|
||||||
|
Resource = type('Resource', (object,), {'fields': fields,
|
||||||
|
'include': (),
|
||||||
|
'exclude': ()})
|
||||||
|
data[fname] = _object_to_data(obj, Resource())
|
||||||
|
else:
|
||||||
data[fname] = _object_to_data(obj)
|
data[fname] = _object_to_data(obj)
|
||||||
|
|
||||||
except NoReverseMatch:
|
except NoReverseMatch:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user