aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/relations.py
diff options
context:
space:
mode:
authorTom Christie2013-01-03 23:17:31 +0000
committerTom Christie2013-01-03 23:17:31 +0000
commit6e9865cb71ff45e90020d3d0dc7c40f20c760d2e (patch)
treed1323ece42b6e0a6fe365dadd7293e8cedcbf2bd /rest_framework/relations.py
parent9b1532b1ea2dcc5d945ebe665093d75bd9d5fa61 (diff)
downloaddjango-rest-framework-6e9865cb71ff45e90020d3d0dc7c40f20c760d2e.tar.bz2
Fix for #446. Note: Also needs applying to other relational types.
Diffstat (limited to 'rest_framework/relations.py')
-rw-r--r--rest_framework/relations.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/rest_framework/relations.py b/rest_framework/relations.py
index 686dcf04..ae0d3de8 100644
--- a/rest_framework/relations.py
+++ b/rest_framework/relations.py
@@ -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