mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-30 05:24:00 +03:00
Added an optional unique field to Album and checked that duplicates are detected.
This commit is contained in:
parent
ef94861c2d
commit
9e291879d1
|
@ -103,7 +103,7 @@ class BlogPostComment(RESTFrameworkModel):
|
|||
|
||||
class Album(RESTFrameworkModel):
|
||||
title = models.CharField(max_length=100, unique=True)
|
||||
|
||||
ref = models.CharField(max_length=10, unique=True, null=True, blank=True)
|
||||
|
||||
class Photo(RESTFrameworkModel):
|
||||
description = models.TextField()
|
||||
|
|
|
@ -611,12 +611,15 @@ class ModelValidationTests(TestCase):
|
|||
"""
|
||||
Just check if serializers.ModelSerializer handles unique checks via .full_clean()
|
||||
"""
|
||||
serializer = AlbumsSerializer(data={'title': 'a'})
|
||||
serializer = AlbumsSerializer(data={'title': 'a', 'ref': '1'})
|
||||
serializer.is_valid()
|
||||
serializer.save()
|
||||
second_serializer = AlbumsSerializer(data={'title': 'a'})
|
||||
self.assertFalse(second_serializer.is_valid())
|
||||
self.assertEqual(second_serializer.errors, {'title': ['Album with this Title already exists.']})
|
||||
self.assertEqual(second_serializer.errors, {'title': ['Album with this Title already exists.'],})
|
||||
third_serializer = AlbumsSerializer(data={'title': 'b', 'ref': '1'})
|
||||
self.assertFalse(third_serializer.is_valid())
|
||||
self.assertEqual(third_serializer.errors, {'ref': ['Album with this Ref already exists.'],})
|
||||
|
||||
def test_foreign_key_is_null_with_partial(self):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user