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
693892ed
Commit
693892ed
authored
Oct 04, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for field to make it easier to access field relationships
parent
cc0d2601
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
1 deletions
+62
-1
rest_framework/fields.py
+5
-1
rest_framework/tests/genericrelations.py
+33
-0
rest_framework/tests/models.py
+24
-0
No files found.
rest_framework/fields.py
View file @
693892ed
...
@@ -83,6 +83,10 @@ class Field(object):
...
@@ -83,6 +83,10 @@ class Field(object):
if
is_protected_type
(
value
):
if
is_protected_type
(
value
):
return
value
return
value
all_callable
=
getattr
(
value
,
'all'
,
None
)
if
is_simple_callable
(
all_callable
):
return
[
self
.
to_native
(
item
)
for
item
in
value
.
all
()]
elif
hasattr
(
value
,
'__iter__'
)
and
not
isinstance
(
value
,
(
dict
,
basestring
)):
elif
hasattr
(
value
,
'__iter__'
)
and
not
isinstance
(
value
,
(
dict
,
basestring
)):
return
[
self
.
to_native
(
item
)
for
item
in
value
]
return
[
self
.
to_native
(
item
)
for
item
in
value
]
return
smart_unicode
(
value
)
return
smart_unicode
(
value
)
...
@@ -197,7 +201,7 @@ class ModelField(WritableField):
...
@@ -197,7 +201,7 @@ class ModelField(WritableField):
value
=
self
.
model_field
.
_get_val_from_obj
(
obj
)
value
=
self
.
model_field
.
_get_val_from_obj
(
obj
)
if
is_protected_type
(
value
):
if
is_protected_type
(
value
):
return
value
return
value
return
self
.
model_field
.
value_to_string
(
self
.
obj
)
return
self
.
model_field
.
value_to_string
(
obj
)
def
attributes
(
self
):
def
attributes
(
self
):
return
{
return
{
...
...
rest_framework/tests/genericrelations.py
0 → 100644
View file @
693892ed
from
django.test
import
TestCase
from
rest_framework
import
serializers
from
rest_framework.tests.models
import
*
class
TestGenericRelations
(
TestCase
):
def
setUp
(
self
):
bookmark
=
Bookmark
(
url
=
'https://www.djangoproject.com/'
)
bookmark
.
save
()
django
=
Tag
(
tag_name
=
'django'
)
django
.
save
()
python
=
Tag
(
tag_name
=
'python'
)
python
.
save
()
t1
=
TaggedItem
(
content_object
=
bookmark
,
tag
=
django
)
t1
.
save
()
t2
=
TaggedItem
(
content_object
=
bookmark
,
tag
=
python
)
t2
.
save
()
self
.
bookmark
=
bookmark
def
test_reverse_generic_relation
(
self
):
class
BookmarkSerializer
(
serializers
.
ModelSerializer
):
tags
=
serializers
.
Field
(
source
=
'tags'
)
class
Meta
:
model
=
Bookmark
exclude
=
(
'id'
,)
serializer
=
BookmarkSerializer
(
instance
=
self
.
bookmark
)
expected
=
{
'tags'
:
[
u'django'
,
u'python'
],
'url'
:
u'https://www.djangoproject.com/'
}
self
.
assertEquals
(
serializer
.
data
,
expected
)
rest_framework/tests/models.py
View file @
693892ed
from
django.db
import
models
from
django.db
import
models
from
django.contrib.contenttypes.models
import
ContentType
from
django.contrib.contenttypes.generic
import
GenericForeignKey
,
GenericRelation
# from django.contrib.auth.models import Group
# from django.contrib.auth.models import Group
...
@@ -59,3 +62,24 @@ class CallableDefaultValueModel(RESTFrameworkModel):
...
@@ -59,3 +62,24 @@ class CallableDefaultValueModel(RESTFrameworkModel):
class
ManyToManyModel
(
RESTFrameworkModel
):
class
ManyToManyModel
(
RESTFrameworkModel
):
rel
=
models
.
ManyToManyField
(
Anchor
)
rel
=
models
.
ManyToManyField
(
Anchor
)
# Models to test generic relations
class
Tag
(
RESTFrameworkModel
):
tag_name
=
models
.
SlugField
()
class
TaggedItem
(
RESTFrameworkModel
):
tag
=
models
.
ForeignKey
(
Tag
,
related_name
=
'items'
)
content_type
=
models
.
ForeignKey
(
ContentType
)
object_id
=
models
.
PositiveIntegerField
()
content_object
=
GenericForeignKey
(
'content_type'
,
'object_id'
)
def
__unicode__
(
self
):
return
self
.
tag
.
tag_name
class
Bookmark
(
RESTFrameworkModel
):
url
=
models
.
URLField
()
tags
=
GenericRelation
(
TaggedItem
)
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