Edit: it seems that when we use a DateTimeField
in django, it expects two inputs, one with the date, and one with the time like this:
So I'll just create two fields, one for the time and one for the date, will post an answer if it works as expected.
I created a custom widget in order to replace the default django's datetime picker in my administration site.
To do so I followed these instructions but it ends up like this when I click save button:
I have no other specific errors in the logs.
For more details, here is the code:
settings.py:
DATE_INPUT_FORMATS = ['%d/%m/%Y %H:%M']
app/templates/widgets/xdsoft_datetimepicker.html
{% include "django/forms/widgets/input.html" %}
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<!-- XDSoft DateTimePicker -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.min.css" integrity="sha256-DOS9W6NR+NFe1fUhEE0PGKY/fubbUCnOfTje2JMDw3Y=" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js" integrity="sha256-FEqEelWI3WouFOo2VWP/uJfs1y8KJ++FLh2Lbqc8SJk=" crossorigin="anonymous"></script>
<script>
$(function () {
$("input[name='{{ widget.name }}']").datetimepicker({
format: 'd/m/Y H:i',
});
});
</script>
app/models.py:
class DatesAndDates(models.Model):
date_time_1 = models.DateTimeField()
app/forms.py:
class DateForm(forms.ModelForm):
class Meta:
model = DatesAndDates
widgets = {
"date_time_1": XDSoftDateTimePickerInput(
)
}
fields=["date_time_1"]
And Finally app/admin.py:
@admin.register(DatesAndDates)
class DatesAndDatesAdmin(admin.ModelAdmin):
form = DateForm
list_display = ["date_time_1"]
Before trying this I just used a Charfield for the date and time and added the jQuery script by overriding the change_form.html template but i failed at converting the datetime string to a valid datetime type.
Thanks for reading and for the help.