mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 14:24:48 +03:00
Use compat implementations for the old meta api methods
This commit is contained in:
parent
2cc3b417fa
commit
0a89478972
|
@ -71,6 +71,30 @@ except ImportError:
|
||||||
JSONField = None
|
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
|
# django-filter is optional
|
||||||
try:
|
try:
|
||||||
import django_filters
|
import django_filters
|
||||||
|
|
|
@ -13,6 +13,10 @@ from django.core.exceptions import ImproperlyConfigured
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
|
from rest_framework.compat import (
|
||||||
|
get_all_related_objects, get_all_related_many_to_many_objects
|
||||||
|
)
|
||||||
|
|
||||||
FieldInfo = namedtuple('FieldResult', [
|
FieldInfo = namedtuple('FieldResult', [
|
||||||
'pk', # Model field instance
|
'pk', # Model field instance
|
||||||
'fields', # Dict of field name -> model field instance
|
'fields', # Dict of field name -> model field instance
|
||||||
|
@ -126,15 +130,7 @@ def _get_reverse_relationships(opts):
|
||||||
# See: https://code.djangoproject.com/ticket/24208
|
# See: https://code.djangoproject.com/ticket/24208
|
||||||
|
|
||||||
reverse_relations = OrderedDict()
|
reverse_relations = OrderedDict()
|
||||||
|
for relation in get_all_related_objects(opts):
|
||||||
# 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()
|
accessor_name = relation.get_accessor_name()
|
||||||
related = getattr(relation, 'related_model', relation.model)
|
related = getattr(relation, 'related_model', relation.model)
|
||||||
reverse_relations[accessor_name] = RelationInfo(
|
reverse_relations[accessor_name] = RelationInfo(
|
||||||
|
@ -144,15 +140,8 @@ def _get_reverse_relationships(opts):
|
||||||
has_through_model=False
|
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.
|
# 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()
|
accessor_name = relation.get_accessor_name()
|
||||||
related = getattr(relation, 'related_model', relation.model)
|
related = getattr(relation, 'related_model', relation.model)
|
||||||
reverse_relations[accessor_name] = RelationInfo(
|
reverse_relations[accessor_name] = RelationInfo(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user