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
b1b58762
Commit
b1b58762
authored
Jan 13, 2014
by
Dustin Farris
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move models.resolve_model to serializers._resolve_model
parent
2332382b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
31 deletions
+35
-31
rest_framework/models.py
+1
-22
rest_framework/serializers.py
+27
-2
rest_framework/tests/test_serializers.py
+7
-7
No files found.
rest_framework/models.py
View file @
b1b58762
import
inspect
# Just to keep things like ./manage.py test happy
from
django.db
import
models
def
resolve_model
(
obj
):
"""
Resolve supplied `obj` to a Django model class.
`obj` must be a Django model class, or a string representation
of one.
String representations should have the format:
'appname.ModelName'
"""
if
type
(
obj
)
==
str
and
len
(
obj
.
split
(
'.'
))
==
2
:
app_name
,
model_name
=
obj
.
split
(
'.'
)
return
models
.
get_model
(
app_name
,
model_name
)
elif
inspect
.
isclass
(
obj
)
and
issubclass
(
obj
,
models
.
Model
):
return
obj
else
:
raise
ValueError
(
"{0} is not a valid Django model"
.
format
(
obj
))
rest_framework/serializers.py
View file @
b1b58762
...
@@ -13,14 +13,15 @@ response content is handled by parsers and renderers.
...
@@ -13,14 +13,15 @@ response content is handled by parsers and renderers.
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
copy
import
copy
import
datetime
import
datetime
import
inspect
import
types
import
types
from
decimal
import
Decimal
from
decimal
import
Decimal
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.paginator
import
Page
from
django.core.paginator
import
Page
from
django.db
import
models
from
django.db
import
models
from
django.forms
import
widgets
from
django.forms
import
widgets
from
django.utils.datastructures
import
SortedDict
from
django.utils.datastructures
import
SortedDict
from
rest_framework.compat
import
get_concrete_model
,
six
from
rest_framework.compat
import
get_concrete_model
,
six
from
rest_framework.models
import
resolve_model
# Note: We do the following so that users of the framework can use this style:
# Note: We do the following so that users of the framework can use this style:
#
#
...
@@ -33,6 +34,27 @@ from rest_framework.relations import *
...
@@ -33,6 +34,27 @@ from rest_framework.relations import *
from
rest_framework.fields
import
*
from
rest_framework.fields
import
*
def
_resolve_model
(
obj
):
"""
Resolve supplied `obj` to a Django model class.
`obj` must be a Django model class itself, or a string
representation of one. Useful in situtations like GH #1225 where
Django may not have resolved a string-based reference to a model in
another model's foreign key definition.
String representations should have the format:
'appname.ModelName'
"""
if
type
(
obj
)
==
str
and
len
(
obj
.
split
(
'.'
))
==
2
:
app_name
,
model_name
=
obj
.
split
(
'.'
)
return
models
.
get_model
(
app_name
,
model_name
)
elif
inspect
.
isclass
(
obj
)
and
issubclass
(
obj
,
models
.
Model
):
return
obj
else
:
raise
ValueError
(
"{0} is not a Django model"
.
format
(
obj
))
def
pretty_name
(
name
):
def
pretty_name
(
name
):
"""Converts 'first_name' to 'First name'"""
"""Converts 'first_name' to 'First name'"""
if
not
name
:
if
not
name
:
...
@@ -657,7 +679,10 @@ class ModelSerializer(Serializer):
...
@@ -657,7 +679,10 @@ class ModelSerializer(Serializer):
if
model_field
.
rel
:
if
model_field
.
rel
:
to_many
=
isinstance
(
model_field
,
to_many
=
isinstance
(
model_field
,
models
.
fields
.
related
.
ManyToManyField
)
models
.
fields
.
related
.
ManyToManyField
)
related_model
=
resolve_model
(
model_field
.
rel
.
to
)
try
:
related_model
=
_resolve_model
(
model_field
.
rel
.
to
)
except
ValueError
as
error_message
:
raise
ImproperlyConfigured
(
error_message
)
if
to_many
and
not
model_field
.
rel
.
through
.
_meta
.
auto_created
:
if
to_many
and
not
model_field
.
rel
.
through
.
_meta
.
auto_created
:
has_through_model
=
True
has_through_model
=
True
...
...
rest_framework/tests/test_
model
s.py
→
rest_framework/tests/test_
serializer
s.py
View file @
b1b58762
from
django.db
import
models
from
django.db
import
models
from
django.test
import
TestCase
from
django.test
import
TestCase
from
rest_framework.
models
import
resolve_model
from
rest_framework.
serializers
import
_
resolve_model
from
rest_framework.tests.models
import
BasicModel
from
rest_framework.tests.models
import
BasicModel
class
ResolveModelTests
(
TestCase
):
class
ResolveModelTests
(
TestCase
):
"""
"""
`resolve_model` should return a Django model class given the
`
_
resolve_model` should return a Django model class given the
provided argument is a Django model class itself, or a properly
provided argument is a Django model class itself, or a properly
formatted string representation of one.
formatted string representation of one.
"""
"""
def
test_resolve_django_model
(
self
):
def
test_resolve_django_model
(
self
):
resolved_model
=
resolve_model
(
BasicModel
)
resolved_model
=
_
resolve_model
(
BasicModel
)
self
.
assertEqual
(
resolved_model
,
BasicModel
)
self
.
assertEqual
(
resolved_model
,
BasicModel
)
def
test_resolve_string_representation
(
self
):
def
test_resolve_string_representation
(
self
):
resolved_model
=
resolve_model
(
'tests.BasicModel'
)
resolved_model
=
_
resolve_model
(
'tests.BasicModel'
)
self
.
assertEqual
(
resolved_model
,
BasicModel
)
self
.
assertEqual
(
resolved_model
,
BasicModel
)
def
test_resolve_non_django_model
(
self
):
def
test_resolve_non_django_model
(
self
):
with
self
.
assertRaises
(
ValueError
):
with
self
.
assertRaises
(
ValueError
):
resolve_model
(
TestCase
)
_
resolve_model
(
TestCase
)
def
test_resolve_
with_
improper_string_representation
(
self
):
def
test_resolve_improper_string_representation
(
self
):
with
self
.
assertRaises
(
ValueError
):
with
self
.
assertRaises
(
ValueError
):
resolve_model
(
'BasicModel'
)
_
resolve_model
(
'BasicModel'
)
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