0

I am using django-rest-framework and trying to use my timezone date as the default for Date field but I am unable to do so. For DateTime field setting the default=timezone.now works perfectly but for Date field it gives the error in serialization.

'Expected a date, but got a datetime. Refusing to coerce'

time = models.DateTimeField(default=timezone.now) This Works

date = models.DateField(default=timezone.now) But this does not work

date = models.DateField(default=timezone.datetime.today) And even this doesn't work.

tayyab_fareed
  • 559
  • 9
  • 15
  • 1
    This is not a duplicate. Please read the question more carefully. – mmla Apr 16 '19 at 21:47
  • @mmla I did a work around by changing all date fields to datetime fields and getting only date part where needed and ignoring time part – tayyab_fareed Apr 19 '19 at 04:24
  • Seeing as this was incorrectly marked as duplicate, I can't leave an answer, but for anyone who also had this error I had the following field in my models: date = models.DateTimeField(auto_now_add = True) I think 'date' might be a reserved field title and changing that to date_time = models.DateTimeField(auto_now_add = True) fixed it for me. (Worked on my machine but deploying to a postgresql db on webfaction introduced it) – Sam Sep 04 '19 at 08:15

0 Answers0