Reputation: 3374
I'm trying to set a date field to a constant date. My "models.py" with the date field:
class Foo(models.Model):
bar = models.DateField()
In my view:
import datetime
from app.models import Foo
foo = Foo(bar=datetime.date('1/1/2021'))
The error:
Exception Type: TypeError
Exception Value: an integer is required (got type str)
Upvotes: 2
Views: 417
Reputation: 4372
Try code below:
import datetime
date_object = datetime.date.fromisoformat('2021-01-01')
foo = Foo(bar=date_object)
See here Documentation
Upvotes: 0
Reputation: 2819
You can try with strptime function of datetime:
import datetime
from app.models import Foo
foo = Foo(bar=datetime.datetime.strptime('1/1/2021',"%d/%m/%Y").date())
Upvotes: 0
Reputation: 438
you are passing a string to date()
method. You should pass integer inside like this:
foo = Foo(bar=datetime.date(2020, 1, 1))
Upvotes: 3