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

posting date Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]] #47

Open
ewilson opened this issue Jan 5, 2015 · 16 comments
Assignees
Labels

Comments

@ewilson
Copy link

ewilson commented Jan 5, 2015

When I save a model that has a JS date (created with new Date()) I get the error:

Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]

The date in the post was 2015-01-05T22:08:37.838Z. So is this:

  • a defect?
  • something I should handle in the ember frontend, to format the date?
  • something I should handle in the django api?

Appreciate any help.

Using:
ember-cli 0.1.5
ember-data 1.0.0-beta.12
ember-django-adapter 0.5
django 1.7.1
djangorestframework 3.0.2
Python 3.4

@ewilson
Copy link
Author

ewilson commented Jan 6, 2015

It seems that the issue is that I am only storing a date in the backend. The date is defined in my model as follows:

event_date = models.DateField(default=date.today)

If I replace this with

event_date = models.DateTimeField(default=datetime.now)

then everything works as expected. Nevertheless, I'd like it if the adapter/DRF could convert the JS Date object to the appropriate day. However, I can imagine that this is simply beyond what the adapter can reasonably do. (How could it know that the API only wants a date?)

Having thought it through, my guess is that there is no way for the adapter to solve my problem. Feel free to close. Comments are welcome.

@ewilson
Copy link
Author

ewilson commented Jan 6, 2015

OK, I was too quick. It turns out that in the DateTimeField case, the API does not return 400, but it does not record the date field at all. So my workaround doesn't work actually. Whether the back-end expects a DateField or a DateTimeField it doesn't quite work. In the first case the POST returns 400. In the second case it is 200, but is silently unsuccessful.

@dustinfarris
Copy link
Owner

The adapter should work with both DateField and DateTimeField without too much headache. I am looking into this.

@dustinfarris dustinfarris self-assigned this Jan 6, 2015
@dustinfarris
Copy link
Owner

@ewilson this is still on my todo list. currently battling some dev issues with ember-cli and bower

@ewilson
Copy link
Author

ewilson commented Jan 8, 2015

@dustinfarris No worries, its not a blocker for my toy project. I appreciate what you are doing.

@benkonrath benkonrath added the bug label Jan 13, 2015
@dustinfarris
Copy link
Owner

I've confirmed this bug. Basically, Ember has one notion of datetime, whereas Django has date and datetime. Ember Data's DateTransform sends a full ISO-8601 datetime value for everything, and DRF blows up if the model's field is a DateField (as opposed to a DateTimeField).

Personally, I think this is a bug in DRF, or at least a deficiency. We will either need to open a PR with DRF (that may or may not get accepted), or add custom date and datetime transforms to the adapter library that can be imported as needed by EDA users.

I'm open to opinions on which way is best, but I'm thinking the custom transforms would be nice quick fix for now.

/cc @benkonrath

@dustinfarris
Copy link
Owner

It looks like the folks over at DRF already gave this some thought: https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L920-L922

@StErMi
Copy link

StErMi commented Feb 10, 2015

Hi I'm using AngularJS and AngularJS Bootstrap UI and the date returned by the JS datepicker is failing with the same error.

On Django the model field is

date_of_birth = models.DateField(null=True, blank=True)

and when I pass it via POST the date is like this

u'1987-09-03T00:00:00.000Z'

but the serializer.is_valid() return that error

@dustinfarris
Copy link
Owner

Hi @StErMi, I'm not sure if it's possible to override that in Angular, but as a workaround you can tell DRF to accept a different format. http://www.django-rest-framework.org/api-guide/settings/#date-and-time-formatting

@StErMi
Copy link

StErMi commented Feb 10, 2015

What I'm passing inside the POST is the ISO-8601 standard representation of a Javascript date.
It's pretty strange that DRF is not parsing it correctly.

@dustinfarris
Copy link
Owner

Yeah, DRF will process that just fine for DateTimeField, but not for DateField.

@StErMi
Copy link

StErMi commented Feb 10, 2015

Is it a bug? Should we report it?

On Tue, Feb 10, 2015 at 7:10 PM, Dustin Farris notifications@github.com
wrote:

Yeah, DRF will process that just fine for DateTimeField, but not for
DateField.


Reply to this email directly or view it on GitHub
#47 (comment)
.

Founder e CEO di SurveyLab
https://surveylab.me/Android Engineer @ musiXmatch
http://www.musixmatch.com/

Skype: e.ricci.designfuture
Twitter: @StErMi https://twitter.com/stermi
Linkedin: https://www.linkedin.com/in/emanuelericci
Google+: https://plus.google.com/u/0/+EmanueleRicci/posts
Hangout: stermi@gmail.com

@dustinfarris
Copy link
Owner

No, it's a design decision. See the link I posted—DRF is explicit about the formats that are accepted, but you can change them in your settings if you need to.

@ewilson
Copy link
Author

ewilson commented Dec 19, 2015

I tried new Date().toJSON().slice(0, 10), and that works. Seems a reasonable workaround.

@dustinfarris
Copy link
Owner

@ewilson thanks. That sounds like a candidate for a "date" transform.

@ariouss
Copy link

ariouss commented Aug 28, 2021

Hello im use reactjs for front-end and django rest framework for back-end and i want react just send date to backend but i dont know how can i do that ? and my datepicker jost input date but react send datetime for backend. any solution ?

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

No branches or pull requests

5 participants