﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29083	DateField returning string instead of date object	Gary Aleixo	nobody	"I'm experiencing an issue that I can't seem to find any resolution too.

My dev environment:

{{{

$ pip freeze | grep Django                                                                                  
Django==1.11.6

$ python --version                                                                                        
Python 3.6.3

$ mysql --version                                                                                         
mysql  Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using  EditLine wrapper

$ uname -r                                                                                                   
4.13.0-32-generic

OS: Ubuntu 17.10 x86_64
}}}


I'm working with a model that has a DateField() defined

{{{
class MyModel(models.Model):
    start_date = models.DateField()
    end_date = models.DateField()
 
    def to_json(self, option=None, section=None):
        val = {
            'start_date': self.start_date.strftime(settings.ISO_DATE) if self.start_date else None,
            'end_date': self.end_date.strftime(settings.ISO_DATE) if self.end_date elses None
        }
        
        return val
}}}


When I retrieve and access that object through a shell instance I am able to call **mymodelobj.start_date** and it will return **a date object**

{{{
In [5]: mymodelobj = MyModel.objects.get(id=379)
In [6]: mymodelobj.start_date
Out[6]: datetime.date(2018, 1, 29)
In [7]: type(mymodelobj.start_date)
Out[7]: datetime.date
In [8]:
}}}

But when the run through the application normally and when accessing the same object through self (self.start_date)** the object is returning a string instead of a date object**:

{{{
ipdb>
> /home/gary/dev/backend/v4/models.py(3499)to_json()
   3498             'cluster': None if self.cluster is None or self.cluster == 0 else self.cluster.to_json(),
-> 3499             'start_date': self.start_date.strftime(settings.ISO_DATE) if self.start_date else None,
   3500             'end_date': self.end_date.strftime(settings.ISO_DATE) if self.end_date else None,

ipdb> self.start_date
'2018-01-29'
ipdb> type(self.start_date)
<class 'str'>

}}}

Which then results in the following error being thrown.

{{{
  File ""/home/gary/dev/backend/v4/models.py"", line 3498, in to_json
    'start_date': self.start_date.strftime(settings.ISO_DATE) if self.start_date else None,
AttributeError: 'str' object has no attribute 'strftime'
}}}

Anyone have any idea as to what's going on?"	Bug	closed	Database layer (models, ORM)	1.11	Normal	invalid	date datetime		Unreviewed	0	0	0	0	0	0
