#29083: DateField() returning string instead of date object
-----------------------------------------+-------------------------------
               Reporter:  Gary Aleixo    |          Owner:  nobody
                   Type:  Bug            |         Status:  new
              Component:  Uncategorized  |        Version:  1.11
               Severity:  Normal         |       Keywords:  date datetime
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+-------------------------------
 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?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29083>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.b0bf2ac0e59291f8157b114d700646d5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to