Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow DateTimeFields to accept strings as input #2656

bacongobbler opened this issue Mar 8, 2015 · 3 comments

allow DateTimeFields to accept strings as input #2656

bacongobbler opened this issue Mar 8, 2015 · 3 comments


Copy link

bacongobbler commented Mar 8, 2015

I found an interesting "bug" with Django Rest Framework along with how Django's DateTimeField works. Given the following TestCase:

class ReadOnlyDateTimeModel(models.Model):
    date = models.DateTimeField()

    def save(self, *args, **kwargs): = '2015-02-03 00:00:00'
        return super(ReadOnlyDateTimeModel, self).save(*args, **kwargs)

class ReadOnlyDateTimeSerializer(serializers.ModelSerializer):
    date = serializers.DateTimeField(read_only=True)

    class Meta:
        model = ReadOnlyDateTimeModel

class ReadOnlyDateTimeView(generics.ListCreateAPIView):
    queryset = ReadOnlyDateTimeModel.objects.all()
    serializer_class = ReadOnlyDateTimeSerializer

class TestReadOnlyFieldSerializer(TestCase):
    def setUp(self):
        self.view = ReadOnlyDateTimeView.as_view()

    def test_model_save_works(self):
        obj = ReadOnlyDateTimeModel.objects.create()
        assert == '2015-02-03 00:00:00'

    def test_post_root_view(self):
        POST requests to ListCreateAPIView should create a new object.
        data = {'price': 1}
        request ='/', data, format='json')
        response = self.view(request).render()
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

I see this error:

tests/ in test_post_root_view
    response = self.view(request).render()
/usr/local/lib/python2.7/site-packages/django/views/decorators/ in wrapped_view
    return view_func(*args, **kwargs)
/usr/local/lib/python2.7/site-packages/django/views/generic/ in view
    return self.dispatch(request, *args, **kwargs)
rest_framework/ in dispatch
    response = self.handle_exception(exc)
rest_framework/ in dispatch
    response = handler(request, *args, **kwargs)
rest_framework/ in post
    return self.create(request, *args, **kwargs)
rest_framework/ in create
    headers = self.get_success_headers(
rest_framework/ in data
    ret = super(Serializer, self).data
rest_framework/ in data
    self._data = self.to_representation(self.instance)
rest_framework/ in to_representation
    ret[field.field_name] = field.to_representation(attribute)
rest_framework/ in to_representation
    value = value.isoformat()
E   AttributeError: 'unicode' object has no attribute 'isoformat'

It seems like Django will accept a formatted string as input, however DRF tries to format the field as a datetime rather than a string, causing the above failure case. If I change = '2015-02-03 00:00:00' to =, it all works as expected.

Copy link

tomchristie commented Mar 23, 2015

Yup, we probably should correctly coerce string values too.
Really the internal value ought to be a datetime instance rather than a raw string, but we should still adopt the more graceful stance.

tomchristie added a commit that referenced this issue May 1, 2015
DateField to_representation can handle str and empty values. Fixes #2656, #2687.
Copy link

luccascorrea commented Oct 29, 2015

The error was not fixed for the DateTimeField, only the DateField. I think this issue should be reopened.

Copy link

tomchristie commented Nov 2, 2015

@luccascorrea Okay, happy to accept a new issue for DateTimeField here (probably best to keep them separate)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants