Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
2acc6a75
Commit
2acc6a75
authored
Nov 18, 2015
by
Andrei Fokau
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use related_objects api for Django 1.9+
parent
378b7b89
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
2 deletions
+35
-2
rest_framework/compat.py
+29
-0
rest_framework/utils/model_meta.py
+6
-2
No files found.
rest_framework/compat.py
View file @
2acc6a75
...
@@ -230,3 +230,32 @@ def template_render(template, context=None, request=None):
...
@@ -230,3 +230,32 @@ def template_render(template, context=None, request=None):
# backends template, e.g. django.template.backends.django.Template
# backends template, e.g. django.template.backends.django.Template
else
:
else
:
return
template
.
render
(
context
,
request
=
request
)
return
template
.
render
(
context
,
request
=
request
)
def
get_all_related_objects
(
opts
):
"""
Django 1.8 changed meta api, see
https://docs.djangoproject.com/en/1.8/ref/models/meta/#migrating-old-meta-api
https://code.djangoproject.com/ticket/12663
https://github.com/django/django/pull/3848
:param opts: Options instance
:return: list of relations except many-to-many ones
"""
if
django
.
VERSION
<
(
1
,
9
):
return
opts
.
get_all_related_objects
()
else
:
return
[
r
for
r
in
opts
.
related_objects
if
not
r
.
field
.
many_to_many
]
def
get_all_related_many_to_many_objects
(
opts
):
"""
Django 1.8 changed meta api, see docstr in compat.get_all_related_objects()
:param opts: Options instance
:return: list of many-to-many relations
"""
if
django
.
VERSION
<
(
1
,
9
):
return
opts
.
get_all_related_many_to_many_objects
()
else
:
return
[
r
for
r
in
opts
.
related_objects
if
r
.
field
.
many_to_many
]
rest_framework/utils/model_meta.py
View file @
2acc6a75
...
@@ -13,6 +13,10 @@ from django.core.exceptions import ImproperlyConfigured
...
@@ -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_many_to_many_objects
,
get_all_related_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
...
@@ -134,7 +138,7 @@ def _get_reverse_relationships(opts):
...
@@ -134,7 +138,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
opts
.
get_all_related_objects
(
):
for
relation
in
get_all_related_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
(
...
@@ -146,7 +150,7 @@ def _get_reverse_relationships(opts):
...
@@ -146,7 +150,7 @@ def _get_reverse_relationships(opts):
)
)
# Deal with reverse many-to-many relationships.
# Deal with reverse many-to-many relationships.
for
relation
in
opts
.
get_all_related_many_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
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment