All Day
All Day

Reputation: 143

Adding Current date to Custom Widget fields

I have a custom widget with Date fields and Time Fields, D/M/Y, H/M(24hour). Now, on create event which is where these are used it come up with their empty_label value that i have setup.

I want this Empty_value to be the current day, current month. BUT i don't know if these will be stored if the user leaves the options alone and clicks save.

So i guess my real question is how would i put the current date /time into the select fields as an option they can select without touching them

Here is part of the code that i think will need to be edited...

class SelectDateTimeWidget(forms.MultiWidget):
    supports_microseconds = False

    def __init__(self, attrs=None, date_format=None, time_format=None):
        widgets = (SelectDateWidget(empty_label=( "Year", "Month", "Day")),
                   SelectTimeWidget(use_seconds=False))
        super(SelectDateTimeWidget, self).__init__(widgets, attrs)

Upvotes: 2

Views: 78

Answers (1)

Antoine Pinsard
Antoine Pinsard

Reputation: 34982

What you want is not to set the current date as "empty label", this would make no sense. Empty is empty, not current date. What you want to do is to select the current date by default, which is done by Form.initial:

from django.utils import timezone

form = YourForm(initial={'the_date_field': timezone.now()})

Upvotes: 1

Related Questions