This commit is contained in:
Vimarsh Chaturvedi 2017-07-10 10:05:57 +00:00 committed by GitHub
commit b3cd6b1bb5
2 changed files with 52 additions and 18 deletions

View File

@ -1112,6 +1112,7 @@ class DateTimeField(Field):
""" """
field_timezone = getattr(self, 'timezone', self.default_timezone()) field_timezone = getattr(self, 'timezone', self.default_timezone())
try:
if (field_timezone is not None) and not timezone.is_aware(value): if (field_timezone is not None) and not timezone.is_aware(value):
try: try:
return timezone.make_aware(value, field_timezone) return timezone.make_aware(value, field_timezone)
@ -1119,6 +1120,8 @@ class DateTimeField(Field):
self.fail('make_aware', timezone=field_timezone) self.fail('make_aware', timezone=field_timezone)
elif (field_timezone is None) and timezone.is_aware(value): elif (field_timezone is None) and timezone.is_aware(value):
return timezone.make_naive(value, utc) return timezone.make_naive(value, utc)
except (ValueError):
self.fail('date')
return value return value
def default_timezone(self): def default_timezone(self):

View File

@ -74,7 +74,8 @@ class RegularFieldsModel(models.Model):
COLOR_CHOICES = (('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')) COLOR_CHOICES = (('red', 'Red'), ('blue', 'Blue'), ('green', 'Green'))
DECIMAL_CHOICES = (('low', decimal.Decimal('0.1')), ('medium', decimal.Decimal('0.5')), ('high', decimal.Decimal('0.9'))) DECIMAL_CHOICES = (
('low', decimal.Decimal('0.1')), ('medium', decimal.Decimal('0.5')), ('high', decimal.Decimal('0.9')))
class FieldOptionsModel(models.Model): class FieldOptionsModel(models.Model):
@ -88,7 +89,8 @@ class FieldOptionsModel(models.Model):
class ChoicesModel(models.Model): class ChoicesModel(models.Model):
choices_field_with_nonstandard_args = models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES, verbose_name='A label') choices_field_with_nonstandard_args = models.DecimalField(max_digits=3, decimal_places=1, choices=DECIMAL_CHOICES,
verbose_name='A label')
class Issue3674ParentModel(models.Model): class Issue3674ParentModel(models.Model):
@ -133,6 +135,7 @@ class TestModelSerializer(TestCase):
Test that trying to use ModelSerializer with Abstract Models Test that trying to use ModelSerializer with Abstract Models
throws a ValueError exception. throws a ValueError exception.
""" """
class AbstractModel(models.Model): class AbstractModel(models.Model):
afield = models.CharField(max_length=255) afield = models.CharField(max_length=255)
@ -158,6 +161,7 @@ class TestRegularFieldMappings(TestCase):
""" """
Model fields should map to their equivalent serializer fields. Model fields should map to their equivalent serializer fields.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = RegularFieldsModel model = RegularFieldsModel
@ -222,6 +226,7 @@ class TestRegularFieldMappings(TestCase):
Properties and methods on the model should be allowed as `Meta.fields` Properties and methods on the model should be allowed as `Meta.fields`
values, and should map to `ReadOnlyField`. values, and should map to `ReadOnlyField`.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = RegularFieldsModel model = RegularFieldsModel
@ -238,6 +243,7 @@ class TestRegularFieldMappings(TestCase):
""" """
Both `pk` and the actual primary key name are valid in `Meta.fields`. Both `pk` and the actual primary key name are valid in `Meta.fields`.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = RegularFieldsModel model = RegularFieldsModel
@ -254,6 +260,7 @@ class TestRegularFieldMappings(TestCase):
""" """
Ensure `extra_kwargs` are passed to generated fields. Ensure `extra_kwargs` are passed to generated fields.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = RegularFieldsModel model = RegularFieldsModel
@ -271,6 +278,7 @@ class TestRegularFieldMappings(TestCase):
""" """
Ensure `extra_kwargs` are passed to generated fields. Ensure `extra_kwargs` are passed to generated fields.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = RegularFieldsModel model = RegularFieldsModel
@ -289,6 +297,7 @@ class TestRegularFieldMappings(TestCase):
Field names that do not map to a model field or relationship should Field names that do not map to a model field or relationship should
raise a configuration errror. raise a configuration errror.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = RegularFieldsModel model = RegularFieldsModel
@ -304,6 +313,7 @@ class TestRegularFieldMappings(TestCase):
Fields that have been declared on the serializer class must be included Fields that have been declared on the serializer class must be included
in the `Meta.fields` if it exists. in the `Meta.fields` if it exists.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
missing = serializers.ReadOnlyField() missing = serializers.ReadOnlyField()
@ -324,6 +334,7 @@ class TestRegularFieldMappings(TestCase):
Fields that have been declared on a parent of the serializer class may Fields that have been declared on a parent of the serializer class may
be excluded from the `Meta.fields` option. be excluded from the `Meta.fields` option.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
missing = serializers.ReadOnlyField() missing = serializers.ReadOnlyField()
@ -940,6 +951,7 @@ class TestDecimalFieldMappings(TestCase):
""" """
Test that a `DecimalField` has no `DecimalValidator`. Test that a `DecimalField` has no `DecimalValidator`.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = DecimalFieldModel model = DecimalFieldModel
@ -954,6 +966,7 @@ class TestDecimalFieldMappings(TestCase):
Test that the `MinValueValidator` is converted to the `min_value` Test that the `MinValueValidator` is converted to the `min_value`
argument for the field. argument for the field.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = DecimalFieldModel model = DecimalFieldModel
@ -968,6 +981,7 @@ class TestDecimalFieldMappings(TestCase):
Test that the `MaxValueValidator` is converted to the `max_value` Test that the `MaxValueValidator` is converted to the `max_value`
argument for the field. argument for the field.
""" """
class TestSerializer(serializers.ModelSerializer): class TestSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = DecimalFieldModel model = DecimalFieldModel
@ -1085,7 +1099,6 @@ class Issue3674Test(TestCase):
self.assertEqual(unicode_repr(TestChildModelSerializer()), child_expected) self.assertEqual(unicode_repr(TestChildModelSerializer()), child_expected)
def test_nonID_PK_foreignkey_model_serializer(self): def test_nonID_PK_foreignkey_model_serializer(self):
class TestChildModelSerializer(serializers.ModelSerializer): class TestChildModelSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Issue3674ChildModel model = Issue3674ChildModel
@ -1135,3 +1148,21 @@ class Test5004UniqueChoiceField(TestCase):
serializer = TestUniqueChoiceSerializer(data={'name': 'choice1'}) serializer = TestUniqueChoiceSerializer(data={'name': 'choice1'})
assert not serializer.is_valid() assert not serializer.is_valid()
assert serializer.errors == {'name': ['unique choice model with this name already exists.']} assert serializer.errors == {'name': ['unique choice model with this name already exists.']}
class Issue5220Model(models.Model):
datetime = models.DateTimeField()
class Issue5220TestCase(TestCase):
def test_should_raise_error_for_invalid_input(self):
class TestSerializer(serializers.ModelSerializer):
class Meta:
model = Issue5220Model
fields = ('__all__')
serializer = TestSerializer(data={
'datetime': '2017-08-16 22:00-24:00',
})
assert not serializer.is_valid()