Skip to content

Fix the Django 1.9 compatibility with the Meta api #3511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 39 additions & 0 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,45 @@ 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()


# Compatibility for the *field* instance returned by either
# the old `Options.get_all_related_objects` or our own implementation above
def get_relation_accessor_name(relation):
if not hasattr(relation, 'get_accessor_name'):
# special case for the `OneToOneField` instances
return relation.name
return relation.get_accessor_name()

def get_relation_field(relation):
if not hasattr(relation, 'get_accessor_name'):
# special case for the `OneToOneField` instances
return relation
return relation.field


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

from rest_framework.compat import (
get_all_related_many_to_many_objects, get_all_related_objects,
get_relation_accessor_name, get_relation_field
)

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

reverse_relations = OrderedDict()
for relation in opts.get_all_related_objects():
accessor_name = relation.get_accessor_name()
for relation in get_all_related_objects(opts):
accessor_name = get_relation_accessor_name(relation)
field = get_relation_field(relation)
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
to_many=relation.field.rel.multiple,
to_many=field.rel.multiple,
has_through_model=False
)

# Deal with reverse many-to-many relationships.
for relation in opts.get_all_related_many_to_many_objects():
accessor_name = relation.get_accessor_name()
for relation in get_all_related_many_to_many_objects(opts):
accessor_name = get_relation_accessor_name(relation)
field = get_relation_field(relation)
related = getattr(relation, 'related_model', relation.model)
reverse_relations[accessor_name] = RelationInfo(
model_field=None,
related_model=related,
to_many=True,
has_through_model=(
(getattr(relation.field.rel, 'through', None) is not None) and
not relation.field.rel.through._meta.auto_created
(getattr(field.rel, 'through', None) is not None) and
not field.rel.through._meta.auto_created
)
)

Expand Down