dustinfarris / ember-django-adapter

Ember CLI addon adapter for Django REST Framework
http://dustinfarris.github.io/ember-django-adapter
MIT License
191 stars 49 forks source link

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

Open ewilson opened 9 years ago

ewilson commented 9 years ago

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:

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

dustinfarris commented 9 years ago

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

ewilson commented 9 years ago

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

dustinfarris commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

StErMi commented 9 years ago

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 https://github.com/dustinfarris/ember-django-adapter/issues/47#issuecomment-73750978 .

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 commented 9 years ago

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 commented 8 years ago

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

dustinfarris commented 8 years ago

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

ariouss commented 3 years ago

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 ?