2

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)

1 Answer 1

1

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()})
Sign up to request clarification or add additional context in comments.

2 Comments

in which file would this want to appear in?
Wherever you instantiate your form. Likely within one of your views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.