Skip to content

Commit 6cf54b0

Browse files
committed
Use compat implementations for the old meta api methods
1 parent 2cc3b41 commit 6cf54b0

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

rest_framework/compat.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ def distinct(queryset, base):
7171
JSONField = None
7272

7373

74+
# Models Options old meta api
75+
# Django 1.8 introduced the `Options.get_fields` method that can be used to
76+
# implement *old* meta api methods
77+
# See: https://docs.djangoproject.com/en/1.9/ref/models/meta/#migrating-from-the-old-api
78+
if django.VERSION >= (1, 8):
79+
def get_all_related_objects(opts):
80+
return [
81+
f for f in opts.get_fields()
82+
if (f.one_to_many or f.one_to_one) and f.auto_created
83+
]
84+
85+
def get_all_related_many_to_many_objects(opts):
86+
return all_related_to_many_objects = [
87+
f for f in opts.get_fields(include_hidden=True)
88+
if f.many_to_many and f.auto_created
89+
]
90+
else:
91+
def get_all_related_objects(opts):
92+
return opts.get_all_related_objects()
93+
94+
def get_all_related_many_to_many_objects(opts):
95+
return opts.get_all_related_many_to_many_objects()
96+
97+
7498
# django-filter is optional
7599
try:
76100
import django_filters

rest_framework/utils/model_meta.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
from django.db import models
1414
from django.utils import six
1515

16+
from rest_framework.compat import (
17+
get_all_related_objects, get_all_related_many_to_many_objects
18+
)
19+
1620
FieldInfo = namedtuple('FieldResult', [
1721
'pk', # Model field instance
1822
'fields', # Dict of field name -> model field instance
@@ -126,15 +130,7 @@ def _get_reverse_relationships(opts):
126130
# See: https://code.djangoproject.com/ticket/24208
127131

128132
reverse_relations = OrderedDict()
129-
130-
# The backward implementation can be found in the Django Documentation
131-
# See: https://docs.djangoproject.com/en/1.9/ref/models/meta/#migrating-from-the-old-api
132-
related_objects = [
133-
f for f in opts.get_fields()
134-
if (f.one_to_many or f.one_to_one) and f.auto_created
135-
]
136-
137-
for relation in related_objects:
133+
for relation in get_all_related_objects(opts):
138134
accessor_name = relation.get_accessor_name()
139135
related = getattr(relation, 'related_model', relation.model)
140136
reverse_relations[accessor_name] = RelationInfo(
@@ -144,15 +140,8 @@ def _get_reverse_relationships(opts):
144140
has_through_model=False
145141
)
146142

147-
# The backward implementation can be found in the Django Documentation
148-
# See: https://docs.djangoproject.com/en/1.9/ref/models/meta/#migrating-from-the-old-api
149-
all_related_to_many_objects = [
150-
f for f in opts.get_fields(include_hidden=True)
151-
if f.many_to_many and f.auto_created
152-
]
153-
154143
# Deal with reverse many-to-many relationships.
155-
for relation in all_related_to_many_objects:
144+
for relation in get_all_related_many_to_many_objects(opts):
156145
accessor_name = relation.get_accessor_name()
157146
related = getattr(relation, 'related_model', relation.model)
158147
reverse_relations[accessor_name] = RelationInfo(

0 commit comments

Comments
 (0)