Helper function to use SplitDateTimeField for datetimes

This commit is contained in:
Ales (Shagi) Zabala Alava 2021-01-11 16:15:57 +01:00
parent 3fd269474a
commit 525dcbf3bb
3 changed files with 28 additions and 1 deletions

View File

@ -55,3 +55,26 @@ class UserForm(forms.ModelForm):
self.save_m2m() self.save_m2m()
return obj return obj
def split_datetime_field(form, field_name):
old_field = form.fields[field_name]
new_field = forms.SplitDateTimeField(
label=old_field.label,
label_suffix=old_field.label_suffix,
required=old_field.required,
help_text=old_field.help_text,
initial=old_field.initial,
validators=old_field.validators,
localize=old_field.localize,
disabled=old_field.disabled,
input_date_formats=("%Y-%m-%d",),
input_time_formats=("%H:%M", "%H:%M:%S"),
widget=forms.SplitDateTimeWidget(
date_format='%Y-%m-%d',
time_format='%H:%M:%S',
time_attrs={'placeholder': '00:00'},
),
)
new_field.widget.widgets[0].input_type = 'date'
form.fields[field_name] = new_field

View File

@ -5,7 +5,6 @@
{% if field.errors %} has-error{% endif %} {% if field.errors %} has-error{% endif %}
"> ">
<label for="{{ field.id_for_label }}">{{ field.label }}</label> <label for="{{ field.id_for_label }}">{{ field.label }}</label>
{% add_widget_attrs field placeholder=field.label %}
{{ field }} {{ field }}
{% if add_another_url %} {% if add_another_url %}

View File

@ -22,6 +22,11 @@ def base_form_field(field, css=None, container_class='', add_another_url=None, f
elif isinstance(field.field, forms.DateField): elif isinstance(field.field, forms.DateField):
field.field.widget.input_type = 'date' field.field.widget.input_type = 'date'
if isinstance(field.field, forms.SplitDateTimeField):
field.field.widget.widgets[1].attrs['placeholder'] = '00:00:00'
else:
field.field.widget.attrs['placeholder'] = field.label
if field_template is None: if field_template is None:
if isinstance(field.field.widget, forms.CheckboxInput): if isinstance(field.field.widget, forms.CheckboxInput):
field_template = 'gas/tags/forms/checkbox.html' field_template = 'gas/tags/forms/checkbox.html'