Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Use backward compatible implementation for the meta api calls
  • Loading branch information
dulacp committed Oct 16, 2015
commit 5a72847b766e3e58b8002d478040274503d5c8a5
19 changes: 17 additions & 2 deletions rest_framework/utils/model_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@ def _get_reverse_relationships(opts):
# See: https://code.djangoproject.com/ticket/24208

reverse_relations = OrderedDict()
for relation in opts.get_all_related_objects():

# The backward implementation can be found in the Django Documentation
# See: https://docs.djangoproject.com/en/1.9/ref/models/meta/#migrating-from-the-old-api
related_objects = [
f for f in opts.get_fields()
if (f.one_to_many or f.one_to_one) and f.auto_created
]

for relation in related_objects:
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
Expand All @@ -136,8 +144,15 @@ def _get_reverse_relationships(opts):
has_through_model=False
)

# The backward implementation can be found in the Django Documentation
# See: https://docs.djangoproject.com/en/1.9/ref/models/meta/#migrating-from-the-old-api
all_related_to_many_objects = [
f for f in opts.get_fields(include_hidden=True)
if f.many_to_many and f.auto_created
]

# Deal with reverse many-to-many relationships.
for relation in opts.get_all_related_many_to_many_objects():
for relation in all_related_to_many_objects:
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
Expand Down