#๐Ÿ”’ Django ModelForm datetime field doesn't load default datetime value into form

6 messages ยท Page 1 of 1 (latest)

mighty oxide
#

I am working with a ModelForm

# forms.py
class AddOrderForm(ModelForm):
    class Meta:
        model = Order
        fields = '__all__'
        widgets = {
            'datetime_ordered': DateTimeInput(attrs={'type': 'datetime-local'}),
            'scheduled_arrival': DateTimeInput(attrs={'type': 'datetime-local'}),
        }

for this model

# models.py
class Order(models.Model):
    customer = models.CharField(max_length=100, verbose_name="Nome cliente", blank=True)
    address = models.CharField(max_length=100, verbose_name="Indirizzo")
    datetime_ordered = models.DateTimeField(default=datetime.datetime.now, verbose_name="Ordine effettuato il")

    as_soon_as_possible = models.BooleanField(default=False, verbose_name="Appena possibile?")
    scheduled_arrival = models.DateTimeField(default=datetime.datetime.now,
                                             blank=True, null=True, verbose_name="Richiesto per")

    def save(self, *args, **kwargs):
        if not self.customer:
            self.customer = "Cliente"
        super(Order, self).save(*args, **kwargs)

    def __str__(self):
        return ", ".join([self.customer, self.address])

and this view

# views.py
class AddOrderView(CreateView):
    model = Order
    form_class = AddOrderForm
    template_name = "add_order.html"

with this template

<!-- add_order.html -->
<form method="POST">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Save"/>
</form>

And I am encountering this problem:

The template doesn't load the model's default datetime values into the form inputs. Does someone know why? Thanks in advance.

What am I trying to do? I am trying to hava a page where I can create objects for my "Order" model.

steep tokenBOT
#

@mighty oxide

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mighty oxide
#

This is what my form looks like rendered in html, but it shouldn't be like that. Datetime fields should have model's default values of datetime_ordered and scheduled_arrival fields.

#

USING Chrome Inspect Tool, this is how the inputs get rendered:

<input type="datetime-local" name="datetime_ordered" value="23/02/2024 05:09:56" required="" id="id_datetime_ordered">

Since the value attribute contains the actual value, is it possible that I should specify the "value format"? Seems like that the html input type datetime-local accepts this format for default value: value="2018-06-12T19:30" This date is an example. Is there a way I can specify the format?

steep tokenBOT
#

@mighty oxide

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.