0

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

artiest
  • 554
  • 5
  • 20

1 Answers1

1

This might probably work:

import datetime

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

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

AM80
  • 36
  • 1
  • 3
  • thank you , but it doesnt work , i've used datepicker-local in my input , it still starts from current hour – artiest Sep 25 '21 at 15:44