20👍
>>> import datetime
>>> myStr = "2011-10-01 15:26"
>>> WHAT_GOES_HERE = datetime.datetime.strptime(myStr, "%Y-%m-%d %H:%M")
>>> WHAT_GOES_HERE
datetime.datetime(2011, 10, 1, 15, 26)
>>>
- Django – disable one of system checks
- How to access directory file outside django project?
- Django Pipeline, Heroku, and SASS
- Django assert failure: assertInHTML('hello', '<html>hello</html>')
- How to check Django security vulnerabilities and how to fix them
1👍
From the Django documentation:
# inserting datetime.now()
import django.utils.timezone as tz
mytable.objects.create(myDate=tz.localtime())
# inserting any date:
import pytz
import django.utils.timezone as tz
from datetime import datetime as dt
my_tz = pytz.timezone('Europe/Bucharest')
my_date = dt(2018, 8, 20, 0)
mytable.objects.create(myDate=tz.make_aware(my_date, my_tz))
👤Tavy
0👍
You can simply do the following
myStr = '2011/10/01 15:26'
And then when creating your object just use myStr as an attribute value:
p = mytable(myDate = myStr)
p.save()
- Mongoengine… query something not in a ListField?
- Making transient (non-database) attributes in Django model available to template
- Pip / virtualenv / django installation issue
- Make group by having count(*) with django orm
Source:stackexchange.com