Skip to content
Closed
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Use compat implementations for the old meta api methods
  • Loading branch information
dulacp committed Oct 16, 2015
commit 0a89478972867d1d5527bfd41d2919d5e753b02c
24 changes: 24 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ def distinct(queryset, base):
JSONField = None


# Models Options old meta api
# Django 1.8 introduced the `Options.get_fields` method that can be used to
# implement *old* meta api methods
# See: https://docs.djangoproject.com/en/1.9/ref/models/meta/#migrating-from-the-old-api
if django.VERSION >= (1, 8):
def get_all_related_objects(opts):
return [
f for f in opts.get_fields()
if (f.one_to_many or f.one_to_one) and f.auto_created
]

def get_all_related_many_to_many_objects(opts):
return [
f for f in opts.get_fields(include_hidden=True)
if f.many_to_many and f.auto_created
]
else:
def get_all_related_objects(opts):
return opts.get_all_related_objects()

def get_all_related_many_to_many_objects(opts):
return opts.get_all_related_many_to_many_objects()


# django-filter is optional
try:
import django_filters
Expand Down
23 changes: 6 additions & 17 deletions rest_framework/utils/model_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from django.db import models
from django.utils import six

from rest_framework.compat import (
get_all_related_objects, get_all_related_many_to_many_objects
)

FieldInfo = namedtuple('FieldResult', [
'pk', # Model field instance
'fields', # Dict of field name -> model field instance
Expand Down Expand Up @@ -126,15 +130,7 @@ def _get_reverse_relationships(opts):
# See: https://code.djangoproject.com/ticket/24208

reverse_relations = OrderedDict()

# 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:
for relation in get_all_related_objects(opts):
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
Expand All @@ -144,15 +140,8 @@ 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 all_related_to_many_objects:
for relation in get_all_related_many_to_many_objects(opts):
accessor_name = relation.get_accessor_name()
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
Expand Down