Date formating
See original GitHub issueJSON doesn’t have a date format so dates in graphene are just string fields. This means any dates that are output without being formatted first (e.g. if you use a DjangoNode with only_fields where one of the fields is a date field) are just output as unicode(date_object)
(which looks like 2016-03-29 17:42:01.683650+00:00
). If you take that date in JavaScript and do new Date(myDateStr)
browsers will probably work it out but it’s not an officially supported date format (as we found out when we tried to parse it in React Native - i.e. Javascript Core on iOS - which doesn’t handle it).
I’m happy to create a pull request but I’m not sure what the best solution would be. Here are some options:
- Make all dates output as
2016-10-01T14:12:00+01:00
which is a format JavaScript understands (which makes sense since we’re outputting JSON)- if we go down this route I’m not sure where the best place to hook it in would be - maybe another special case in coerce_string in graphql-core like there is for booleans?
- another option would be to just do it for Django fields - make a DateTimeString type with a separate serialize function and modify convert_django_field for the DateField
- Do one of the above but make it configurable somehow
- If those are’t options (e.g. for backwards compatibility) then it would be nice to be able to hook in and do it ourselves without having to think about a fix every time we encounter a date field
Happy to do the work. If you’d like that, let me know which option you’d prefer and I’ll open a PR.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:14 (4 by maintainers)
Top GitHub Comments
I’ve solved the
AssertionError: Received not compatible datetime "'2018-01-20 09:35:53+00:00'"
issue by creating and using a custom type that overridesserialize
and parses the string datetime:I finally added complete support for conversion from
DateField
to aDateTime
scalar in graphene that will output the python date in a ISO-8601 format (Javascript Date format).You can update
graphene
to the last version0.8.0
and it will be working as expected.