Opened 7 years ago
Closed 7 years ago
#29083 closed Bug (invalid)
DateField returning string instead of date object
Reported by: | Gary Aleixo | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | date datetime |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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?
Change History (1)
comment:1 by , 7 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Resolution: | → invalid |
Status: | new → closed |
Summary: | DateField() returning string instead of date object → DateField returning string instead of date object |
Please see TicketClosingReasons/UseSupportChannels for places to get help debugging your code. If you confirm a bug in Django, please reopen the ticket with steps to reproduce.