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
6e9865cb
Commit
6e9865cb
authored
Jan 03, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for #446. Note: Also needs applying to other relational types.
parent
9b1532b1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
2 deletions
+37
-2
rest_framework/relations.py
+19
-2
rest_framework/tests/fields.py
+18
-0
No files found.
rest_framework/relations.py
View file @
6e9865cb
...
...
@@ -4,6 +4,7 @@ from django import forms
from
django.forms
import
widgets
from
django.forms.models
import
ModelChoiceIterator
from
django.utils.encoding
import
smart_unicode
from
django.utils.translation
import
ugettext_lazy
as
_
from
rest_framework.fields
import
Field
,
WritableField
from
rest_framework.reverse
import
reverse
from
urlparse
import
urlparse
...
...
@@ -168,6 +169,11 @@ class PrimaryKeyRelatedField(RelatedField):
default_read_only
=
False
form_field_class
=
forms
.
ChoiceField
default_error_messages
=
{
'does_not_exist'
:
_
(
"Invalid pk '
%
s' - object does not exist."
),
'invalid'
:
_
(
'Invalid value.'
),
}
# TODO: Remove these field hacks...
def
prepare_value
(
self
,
obj
):
return
self
.
to_native
(
obj
.
pk
)
...
...
@@ -193,7 +199,10 @@ class PrimaryKeyRelatedField(RelatedField):
try
:
return
self
.
queryset
.
get
(
pk
=
data
)
except
ObjectDoesNotExist
:
msg
=
"Invalid pk '
%
s' - object does not exist."
%
smart_unicode
(
data
)
msg
=
self
.
error_messages
[
'does_not_exist'
]
%
smart_unicode
(
data
)
raise
ValidationError
(
msg
)
except
(
TypeError
,
ValueError
):
msg
=
self
.
error_messages
[
'invalid'
]
raise
ValidationError
(
msg
)
def
field_to_native
(
self
,
obj
,
field_name
):
...
...
@@ -215,6 +224,11 @@ class ManyPrimaryKeyRelatedField(ManyRelatedField):
default_read_only
=
False
form_field_class
=
forms
.
MultipleChoiceField
default_error_messages
=
{
'does_not_exist'
:
_
(
"Invalid pk '
%
s' - object does not exist."
),
'invalid'
:
_
(
'Invalid value.'
),
}
def
prepare_value
(
self
,
obj
):
return
self
.
to_native
(
obj
.
pk
)
...
...
@@ -249,7 +263,10 @@ class ManyPrimaryKeyRelatedField(ManyRelatedField):
try
:
return
self
.
queryset
.
get
(
pk
=
data
)
except
ObjectDoesNotExist
:
msg
=
"Invalid pk '
%
s' - object does not exist."
%
smart_unicode
(
data
)
msg
=
self
.
error_messages
[
'does_not_exist'
]
%
smart_unicode
(
data
)
raise
ValidationError
(
msg
)
except
(
TypeError
,
ValueError
):
msg
=
self
.
error_messages
[
'invalid'
]
raise
ValidationError
(
msg
)
### Slug relationships
...
...
rest_framework/tests/fields.py
0 → 100644
View file @
6e9865cb
from
django.db
import
models
from
django.test
import
TestCase
from
rest_framework
import
serializers
class
NullModel
(
models
.
Model
):
pass
class
FieldTests
(
TestCase
):
def
test_pk_related_field_with_empty_string
(
self
):
"""
Regression test for #446
https://github.com/tomchristie/django-rest-framework/issues/446
"""
field
=
serializers
.
PrimaryKeyRelatedField
(
queryset
=
NullModel
.
objects
.
all
())
self
.
assertRaises
(
serializers
.
ValidationError
,
field
.
from_native
,
(
''
,))
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