Commit 5d7d51ed by Michal Dvorak

Merge remote-tracking branch 'upstream/master'

parents 5ba2437f ed09d26f
...@@ -150,7 +150,7 @@ Provides a base view for acting on a single object, by combining REST framework' ...@@ -150,7 +150,7 @@ Provides a base view for acting on a single object, by combining REST framework'
* `queryset` - The queryset that should be used when retrieving an object from this view. If unset, defaults to the default queryset manager for `self.model`. * `queryset` - The queryset that should be used when retrieving an object from this view. If unset, defaults to the default queryset manager for `self.model`.
* `pk_kwarg` - The URL kwarg that should be used to look up objects by primary key. Defaults to `'pk'`. [Can only be set to non-default on Django 1.4+] * `pk_kwarg` - The URL kwarg that should be used to look up objects by primary key. Defaults to `'pk'`. [Can only be set to non-default on Django 1.4+]
* `slug_kwarg` - The URL kwarg that should be used to look up objects by a slug. Defaults to `'slug'`. [Can only be set to non-default on Django 1.4+] * `slug_url_kwarg` - The URL kwarg that should be used to look up objects by a slug. Defaults to `'slug'`. [Can only be set to non-default on Django 1.4+]
* `slug_field` - The field on the model that should be used to look up objects by a slug. If used, this should typically be set to a field with `unique=True`. Defaults to `'slug'`. * `slug_field` - The field on the model that should be used to look up objects by a slug. If used, this should typically be set to a field with `unique=True`. Defaults to `'slug'`.
--- ---
......
...@@ -6,6 +6,10 @@ ...@@ -6,6 +6,10 @@
## 2.1.x series ## 2.1.x series
### Master
* Bugfix: Related fields now respect the required flag, and may be required=False.
### 2.1.12 ### 2.1.12
**Date**: 21st Dec 2012 **Date**: 21st Dec 2012
......
...@@ -357,7 +357,12 @@ class RelatedField(WritableField): ...@@ -357,7 +357,12 @@ class RelatedField(WritableField):
if self.read_only: if self.read_only:
return return
value = data.get(field_name) try:
value = data[field_name]
except KeyError:
if self.required:
raise ValidationError(self.error_messages['required'])
return
if value in (None, '') and not self.null: if value in (None, '') and not self.null:
raise ValidationError('Value may not be null') raise ValidationError('Value may not be null')
......
...@@ -308,6 +308,38 @@ class ModelValidationTests(TestCase): ...@@ -308,6 +308,38 @@ class ModelValidationTests(TestCase):
self.assertFalse(second_serializer.is_valid()) self.assertFalse(second_serializer.is_valid())
self.assertEqual(second_serializer.errors, {'title': [u'Album with this Title already exists.']}) self.assertEqual(second_serializer.errors, {'title': [u'Album with this Title already exists.']})
def test_foreign_key_with_partial(self):
"""
Test ModelSerializer validation with partial=True
Specifically test foreign key validation.
"""
album = Album(title='test')
album.save()
class PhotoSerializer(serializers.ModelSerializer):
class Meta:
model = Photo
photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk})
self.assertTrue(photo_serializer.is_valid())
photo = photo_serializer.save()
# Updating only the album (foreign key)
photo_serializer = PhotoSerializer(instance=photo, data={'album': album.pk}, partial=True)
self.assertTrue(photo_serializer.is_valid())
self.assertTrue(photo_serializer.save())
# Updating only the description
photo_serializer = PhotoSerializer(instance=photo,
data={'description': 'new'},
partial=True)
self.assertTrue(photo_serializer.is_valid())
self.assertTrue(photo_serializer.save())
class RegexValidationTest(TestCase): class RegexValidationTest(TestCase):
def test_create_failed(self): def test_create_failed(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment