Answers for "django datetimefield to date"

1

django datefield options

DateField(**options)
auto_now #Set the field to now every time the object is saved
auto_now_add #Set the field to now when the object is first created
Posted by: Guest on September-22-2021
0

string to datetimefield in django

>>> user = User.objects.get(id=1)
>>> date_str = "2018-03-11"
>>> from django.utils.dateparse import parse_date // Way 1
>>> temp_date = parse_date(date_str)
>>> a1 = Article(headline="String converted to date", pub_date=temp_date, reporter=user)
>>> a1.save()
>>> a1.pub_date
datetime.date(2018, 3, 11)
>>> from datetime import datetime // Way 2
>>> temp_date = datetime.strptime(date_str, "%Y-%m-%d").date()
>>> a2 = Article(headline="String converted to date way 2", pub_date=temp_date, reporter=user)
>>> a2.save()
>>> a2.pub_date
datetime.date(2018, 3, 11)
Posted by: Guest on July-08-2021

Code answers related to "django datetimefield to date"

Python Answers by Framework

Browse Popular Code Answers by Language