artiest
artiest

Reputation: 592

how to set default hour for my django datetimefield

im trying to set default hour to my django datetimefield , im using datetime-local in my input , but it shows current time , i want to set default time to 12:00 PM and changeable as well i tried this but doesnt work

from datetime import datetime

def set_default_time_checkout():
    now = datetime.now()
    new_time = now.replace(hour=12, minute=0, second=0, microsecond=0)
    return new_time

class Booking(models.Model):
    check_in = models.DateTimeField(default=timezone.now)
    check_out = models.DateTimeField(default=set_default_time_checkout,blank=True,null=True)

but doesnt work and this is my forms.py

class BookingForm(forms.ModelForm):
    check_in = forms.DateTimeField(required=True,input_formats=['%Y-%m-%dT%H:%M','%Y-%m-%dT%H:M%Z'],widget=forms.DateTimeInput(attrs={'type':'datetime-local'}))
    check_out = forms.DateTimeField(initial=set_default_time_checkout, required=False,input_formats=['%Y-%m-%dT%H:%M','%Y-%m-%dT%H:M%Z'],widget=forms.DateTimeInput(attrs={'type':'datetime-local'}))

also the initial time doesnt work in the modelform is it possible please ? how to achieve it ? i tried these solutions from the question but none of them works Can I set a specific default time for a Django datetime field? thanks in advance

Upvotes: 0

Views: 629

Answers (1)

AM80
AM80

Reputation: 36

This might probably work:

import datetime

defualt=datetime.time(12, 0, 0)

The output of datetime.time() has hour, minute and second instances

Upvotes: 1

Related Questions