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
b783887c
Commit
b783887c
authored
Jan 25, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test for GFK, using RelatedField. Refs #607.
parent
b73d7e9b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
5 deletions
+52
-5
rest_framework/tests/genericrelations.py
+52
-5
No files found.
rest_framework/tests/genericrelations.py
View file @
b783887c
...
@@ -12,7 +12,7 @@ class Tag(models.Model):
...
@@ -12,7 +12,7 @@ class Tag(models.Model):
tag
=
models
.
SlugField
()
tag
=
models
.
SlugField
()
content_type
=
models
.
ForeignKey
(
ContentType
)
content_type
=
models
.
ForeignKey
(
ContentType
)
object_id
=
models
.
PositiveIntegerField
()
object_id
=
models
.
PositiveIntegerField
()
content_object
=
GenericForeignKey
(
'content_type'
,
'object_id'
)
tagged_item
=
GenericForeignKey
(
'content_type'
,
'object_id'
)
def
__unicode__
(
self
):
def
__unicode__
(
self
):
return
self
.
tag
return
self
.
tag
...
@@ -25,20 +25,37 @@ class Bookmark(models.Model):
...
@@ -25,20 +25,37 @@ class Bookmark(models.Model):
url
=
models
.
URLField
()
url
=
models
.
URLField
()
tags
=
GenericRelation
(
Tag
)
tags
=
GenericRelation
(
Tag
)
def
__unicode__
(
self
):
return
'Bookmark:
%
s'
%
self
.
url
class
Note
(
models
.
Model
):
"""
A textual note that may have multiple tags attached.
"""
text
=
models
.
TextField
()
tags
=
GenericRelation
(
Tag
)
def
__unicode__
(
self
):
return
'Note:
%
s'
%
self
.
text
class
TestGenericRelations
(
TestCase
):
class
TestGenericRelations
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
bookmark
=
Bookmark
.
objects
.
create
(
url
=
'https://www.djangoproject.com/'
)
self
.
bookmark
=
Bookmark
.
objects
.
create
(
url
=
'https://www.djangoproject.com/'
)
Tag
.
objects
.
create
(
content_object
=
self
.
bookmark
,
tag
=
'django'
)
Tag
.
objects
.
create
(
tagged_item
=
self
.
bookmark
,
tag
=
'django'
)
Tag
.
objects
.
create
(
content_object
=
self
.
bookmark
,
tag
=
'python'
)
Tag
.
objects
.
create
(
tagged_item
=
self
.
bookmark
,
tag
=
'python'
)
self
.
note
=
Note
.
objects
.
create
(
text
=
'Remember the milk'
)
Tag
.
objects
.
create
(
tagged_item
=
self
.
note
,
tag
=
'reminder'
)
def
test_
reverse_
generic_relation
(
self
):
def
test_generic_relation
(
self
):
"""
"""
Test a relationship that spans a GenericRelation field.
Test a relationship that spans a GenericRelation field.
IE. A reverse generic relationship.
"""
"""
class
BookmarkSerializer
(
serializers
.
ModelSerializer
):
class
BookmarkSerializer
(
serializers
.
ModelSerializer
):
tags
=
serializers
.
ManyRelatedField
(
source
=
'tags'
)
tags
=
serializers
.
ManyRelatedField
()
class
Meta
:
class
Meta
:
model
=
Bookmark
model
=
Bookmark
...
@@ -50,3 +67,33 @@ class TestGenericRelations(TestCase):
...
@@ -50,3 +67,33 @@ class TestGenericRelations(TestCase):
'url'
:
u'https://www.djangoproject.com/'
'url'
:
u'https://www.djangoproject.com/'
}
}
self
.
assertEquals
(
serializer
.
data
,
expected
)
self
.
assertEquals
(
serializer
.
data
,
expected
)
def
test_generic_fk
(
self
):
"""
Test a relationship that spans a GenericForeignKey field.
IE. A forward generic relationship.
"""
class
TagSerializer
(
serializers
.
ModelSerializer
):
tagged_item
=
serializers
.
RelatedField
()
class
Meta
:
model
=
Tag
exclude
=
(
'id'
,
'content_type'
,
'object_id'
)
serializer
=
TagSerializer
(
Tag
.
objects
.
all
())
expected
=
[
{
'tag'
:
u'django'
,
'tagged_item'
:
u'Bookmark: https://www.djangoproject.com/'
},
{
'tag'
:
u'python'
,
'tagged_item'
:
u'Bookmark: https://www.djangoproject.com/'
},
{
'tag'
:
u'reminder'
,
'tagged_item'
:
u'Note: Remember the milk'
}
]
self
.
assertEquals
(
serializer
.
data
,
expected
)
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