33

I have a model for events which almost always start at 10:00pm, but may on occasion start earlier/later. To make things easy in the admin, I'd like for the time to default to 10pm, but be changeable if needed; the date will need to be set regardless, so it doesn't need a default, but ideally it would default to the current date.

I realize that I can use datetime.now to accomplish the latter, but is it possible (and how) to I set the time to a specific default value?

Update: I'm getting answers faster than I can figure out which one(s) does what I'm trying to accomplish...I probably should have been further along with the app before I asked. Thanks for the help in the meantime!

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
lisaq
  • 431
  • 1
  • 4
  • 5
  • I'm getting 'TypeError: __init__() got an unexpected keyword argument 'default''. Any ideas? Using a forms.Form instead of a model form. forms.TimeField(default=dt.time(16, 0), widget=forms.TimeInput) – Turtles Are Cute Apr 16 '16 at 22:22
  • did you fix it please ? if yes , can share the code please – artiest Sep 25 '21 at 12:41

6 Answers6

23

From the Django documents for Field.default:

The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.

So do this:

from datetime import datetime, timedelta

def default_start_time():
    now = datetime.now()
    start = now.replace(hour=22, minute=0, second=0, microsecond=0)
    return start if start > now else start + timedelta(days=1)  

class Something(models.Model):
    timestamp = models.DateTimeField(default=default_start_time)
tobych
  • 2,941
  • 29
  • 18
  • 1
    I like how this example uses "tomorrow at 10pm" if the model instance is created after 10pm. – gldnspud Apr 16 '13 at 23:55
  • when you are using `datetime.now()` instead of `datetime.now` `now` will be a fixed date, when that is first evaluated. What does that mean? When your server runs for a weak, with `datetime.now()` it will always be the date when the server was started. With using `datetime.now` it will be dynamicely evaluated. – Asqiir Apr 30 '17 at 10:34
  • is it possible to just show the hours and minutes when displayed on the screen rather than show the hours minutes and milliseconds. and rather than use military time, use am and pm... because with what you have above it shows the full time. – Omar Jandali Dec 11 '17 at 22:47
18

in case you're interested into setting default value to TimeField: https://code.djangoproject.com/ticket/6754

don't:

start = models.TimeField(default='20:00')

do instead:

import datetime
start = models.TimeField(default=datetime.time(16, 00))
Bruno Gelb
  • 5,322
  • 8
  • 35
  • 50
  • 1
    Not sure why you'd want to include datetime.datetime in a `TimeField`. Better to do `start= models.TimeField(default=datetime.time(16, 00))` – SaeX Oct 18 '15 at 11:31
4

datetime.time(16, 00) does not work.

Use datetime.time(datetime.now()) instead if you are trying to get the current time or datetime.time(your_date_time)

Where your_date_time = datetime.datetime object

Oluwafemi Tairu
  • 960
  • 1
  • 8
  • 12
1

Have you seen this? https://docs.djangoproject.com/en/dev/ref/models/fields/#default

That's probably what you're looking for.

It's also discussed here: Default value for field in Django model

Community
  • 1
  • 1
davidtheclark
  • 4,666
  • 6
  • 32
  • 42
0

import datetime

default_time = datetime.datetime.now().time()

time_of_visit = models.TimeField(default=default_time)

it will be work fine

-2

Something like this?

import datetime

dt = datetime.now()
dt.hour = 22
dt.minute = 0
dt.second = 0

Hard to be more specific without context.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • 1
    I'm not sure what more context I could give; I just want the person entering the date in to not have to change it to 10:00 every single time they add a new event (but still have the option to change it). The solution is admittedly simple if there is one; that looks like it might work. – lisaq Apr 16 '13 at 02:19