mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-17 12:00:57 +03:00
Merge remote-tracking branch 'upstream/master' into writable-nested-modelserializer
This commit is contained in:
commit
2ed79b6dc6
|
@ -141,6 +141,7 @@ The following people have helped make REST framework great.
|
|||
* David Medina - [copitux]
|
||||
* Areski Belaid - [areski]
|
||||
* Ethan Freman - [mindlace]
|
||||
* David Sanders - [davesque]
|
||||
|
||||
Many thanks to everyone who's contributed to the project.
|
||||
|
||||
|
@ -318,3 +319,5 @@ You can also contact [@_tomchristie][twitter] directly on twitter.
|
|||
[copitux]: https://github.com/copitux
|
||||
[areski]: https://github.com/areski
|
||||
[mindlace]: https://github.com/mindlace
|
||||
[davesque]: https://github.com/davesque
|
||||
|
||||
|
|
|
@ -336,9 +336,13 @@ class ModelField(WritableField):
|
|||
raise ValueError("ModelField requires 'model_field' kwarg")
|
||||
|
||||
self.min_length = kwargs.pop('min_length',
|
||||
getattr(self.model_field, 'min_length', None))
|
||||
getattr(self.model_field, 'min_length', None))
|
||||
self.max_length = kwargs.pop('max_length',
|
||||
getattr(self.model_field, 'max_length', None))
|
||||
getattr(self.model_field, 'max_length', None))
|
||||
self.min_value = kwargs.pop('min_value',
|
||||
getattr(self.model_field, 'min_value', None))
|
||||
self.max_value = kwargs.pop('max_value',
|
||||
getattr(self.model_field, 'max_value', None))
|
||||
|
||||
super(ModelField, self).__init__(*args, **kwargs)
|
||||
|
||||
|
@ -346,6 +350,10 @@ class ModelField(WritableField):
|
|||
self.validators.append(validators.MinLengthValidator(self.min_length))
|
||||
if self.max_length is not None:
|
||||
self.validators.append(validators.MaxLengthValidator(self.max_length))
|
||||
if self.min_value is not None:
|
||||
self.validators.append(validators.MinValueValidator(self.min_value))
|
||||
if self.max_value is not None:
|
||||
self.validators.append(validators.MaxValueValidator(self.max_value))
|
||||
|
||||
def from_native(self, value):
|
||||
rel = getattr(self.model_field, "rel", None)
|
||||
|
|
|
@ -212,7 +212,7 @@ class GenericAPIView(views.APIView):
|
|||
You may want to override this if you need to provide different
|
||||
serializations depending on the incoming request.
|
||||
|
||||
(Eg. admins get full serialization, others get basic serilization)
|
||||
(Eg. admins get full serialization, others get basic serialization)
|
||||
"""
|
||||
serializer_class = self.serializer_class
|
||||
if serializer_class is not None:
|
||||
|
|
|
@ -866,3 +866,33 @@ class FieldCallableDefault(TestCase):
|
|||
into = {}
|
||||
field.field_from_native({}, {}, 'field', into)
|
||||
self.assertEqual(into, {'field': 'foo bar'})
|
||||
|
||||
|
||||
class CustomIntegerField(TestCase):
|
||||
"""
|
||||
Test that custom fields apply min_value and max_value constraints
|
||||
"""
|
||||
def test_custom_fields_can_be_validated_for_value(self):
|
||||
|
||||
class MoneyField(models.PositiveIntegerField):
|
||||
pass
|
||||
|
||||
class EntryModel(models.Model):
|
||||
bank = MoneyField(validators=[validators.MaxValueValidator(100)])
|
||||
|
||||
class EntrySerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = EntryModel
|
||||
|
||||
entry = EntryModel(bank=1)
|
||||
|
||||
serializer = EntrySerializer(entry, data={"bank": 11})
|
||||
self.assertTrue(serializer.is_valid())
|
||||
|
||||
serializer = EntrySerializer(entry, data={"bank": -1})
|
||||
self.assertFalse(serializer.is_valid())
|
||||
|
||||
serializer = EntrySerializer(entry, data={"bank": 101})
|
||||
self.assertFalse(serializer.is_valid())
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user