The django-bootstrap-datepicker-plus is not working properly when the date format option is used to set up a custom date format

82 Views Asked by At

I'm using this django model using a DateField with a default:

from django.db import models
from django.utils import timezone

class MyModel(models.Model):
    submitted_by = models.ForeignKey(
        User,
        null=True,
        on_delete=models.SET_NULL,
        verbose_name=_("Submitted by"),
        related_name="submitted_by",
    )
    submitted_at = models.DateField(
        default=timezone.now,
        verbose_name=_("Submission date"),
    )

along with this form:

from bootstrap_datepicker_plus.widgets import DatePickerInput
from django import forms

from .app.models import MyModel

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = [
            "submitted_by",
            "submitted_at",
        ]

        widgets = {
            "submitted_at": DatePickerInput(
                options={
                    #"format": "DD-MM-YYYY", # <---- line to uncomment
                    "locale": "fr-FR",
                    "useCurrent": False,
                }
            ),
        }
    
    def clean(self):
        cleaned_data = super().clean()
        for i, (k,v) in enumerate(cleaned_data.items()):
            print(f"{i}) {k}: {v} type {type(v)}")

        return cleaned_data

Everything is fine, except that the date in my form widget is in the American style: MM/DD/YYYY which I do not want. That's why I've added the "format" option, but once this option is set up (line to be uncommented in the above code), my form is no more valid: the date field becomes red when I submit the form data, but no error message is raised on the server side. The data is simply not submitted to the database. And the print you can see in the clean() function no longer returns anything related to the submitted_at field; it only prints:

0) submitted_by: user-1 type <class 'django.contrib.auth.models.User'>

But I can see the form data QueryDict having this element 'submitted_at': ['29-12-2023'], whereas, before, when the "format" option was commented out, it printed:

0) submitted_by: user-1 type <class 'django.contrib.auth.models.User'>
1) submitted_at: 2023-12-29 type <class 'datetime.date'>

How can I ensure that the format of my date field is respected in the form and that the date remains valid according to the model so that it is correctly transmitted to the database? The django-bootstrap-datepicker-plus documentation is not super verbose about the available options.

Please notice that if I override the submitted_at field at the top of the form definition with:

    submitted_at = forms.DateField(
        initial=timezone.now(),
        widget=DatePickerInput(
            options={
                "format": "DD-MM-YYYY",
                "locale": "fr-FR",
                "useCurrent": False,
            }
        ),
    )

(and that I remove the previous widget definition in the code above)

Then it works. But this means I have to define both in the model and the form some same values such as the default value, the label or the help text. Which is annoying.

django.__version__ is '4.2.7'
bootstrap_datepicker_plus.__version__ is '4.0.0'

0

There are 0 best solutions below