I came across an interesting situation when using this class:
class Company(models.Model):
date = models.DateField()
time = models.TimeField()
c = Company(date=datetime.datetime.now(), time=datetime.datetime.now())
Django decides to use DATETIME_INPUT_FORMATS defined within the formats.py file.
Which makes sense, because I am passing in a datetime.now() to both fields.
I think I could make Django to use DATE_INPUT_FORMATS and TIME_INPUT_FORMATS respectively, if I passed in only the current date and current time in.
Something like this:
c = Company(date=datetime.date.now(), time=datetime.time.now())
But this obviously throws an exception as now doesn’t exist like that. Is there a different way to achieve this?
For the date, you can use
datetime.date.today()ordatetime.datetime.now().date().For the time, you can use
datetime.datetime.now().time().However, why have separate fields for these in the first place? Why not use a single
DateTimeField?You can always define helper functions on the model that return the
.date()or.time()later if you only want one or the other.