Add min_value=0 to autogenerated Pos..IntFields

This commit is contained in:
Stephan Groß 2013-05-18 17:04:20 +02:00
parent 3f47eb7a77
commit 0c81d04170
2 changed files with 77 additions and 0 deletions

View File

@ -739,6 +739,10 @@ class ModelSerializer(Serializer):
if issubclass(model_field.__class__, models.TextField):
kwargs['widget'] = widgets.Textarea
if issubclass(model_field.__class__, models.PositiveIntegerField) or\
issubclass(model_field.__class__, models.PositiveSmallIntegerField):
kwargs['min_value'] = 0
# TODO: TypedChoiceField?
if model_field.flatchoices: # This ModelField contains choices
kwargs['choices'] = model_field.flatchoices

View File

@ -1402,3 +1402,76 @@ class AttributeMappingOnAutogeneratedFieldsTests(TestCase):
def test_url_field(self):
self.field_test('url_field')
class DefaultValuesOnAutogeneratedFieldsTests(TestCase):
def setUp(self):
class DVOAFModel(RESTFrameworkModel):
positive_integer_field = models.PositiveIntegerField(blank=True)
positive_small_integer_field = models.PositiveSmallIntegerField(blank=True)
email_field = models.EmailField(blank=True)
file_field = models.FileField(blank=True)
image_field = models.ImageField(blank=True)
slug_field = models.SlugField(blank=True)
url_field = models.URLField(blank=True)
class DVOAFSerializer(serializers.ModelSerializer):
class Meta:
model = DVOAFModel
self.serializer_class = DVOAFSerializer
self.fields_attributes = {
'positive_integer_field': [
('min_value', 0),
],
'positive_small_integer_field': [
('min_value', 0),
],
'email_field': [
('max_length', 75),
],
'file_field': [
('max_length', 100),
],
'image_field': [
('max_length', 100),
],
'slug_field': [
('max_length', 50),
],
'url_field': [
('max_length', 200),
],
}
def field_test(self, field):
serializer = self.serializer_class(data={})
self.assertEqual(serializer.is_valid(), True)
for attribute in self.fields_attributes[field]:
self.assertEqual(
getattr(serializer.fields[field], attribute[0]),
attribute[1]
)
def test_positive_integer_field(self):
self.field_test('positive_integer_field')
def test_positive_small_integer_field(self):
self.field_test('positive_small_integer_field')
def test_email_field(self):
self.field_test('email_field')
def test_file_field(self):
self.field_test('file_field')
def test_image_field(self):
self.field_test('image_field')
def test_slug_field(self):
self.field_test('slug_field')
def test_url_field(self):
self.field_test('url_field')