#๐Ÿ”’ Django model form doesn't load model's default datetime value into form

20 messages ยท Page 1 of 1 (latest)

thorny igloo
#

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.

warped mapleBOT
#

@thorny igloo

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.

thorny igloo
#

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?

vestal lantern
#

?

thorny igloo
#

I think I can use the format parameter

#

I will try in a second

#

I was able to update the "value" key format it html

#

which now looks like this

<input type="datetime-local" name="datetime_ordered" value="23/02/2024T12:26" required="" id="id_datetime_ordered">
#

but still my datetime inputs are empty

vestal lantern
#

oh

thorny igloo
#

okay I solved

vestal lantern
#

i dont know if i am qualified to answer but am also beginner instead of using all fields in ur model form why cant u exclude date field and use the model datetime to prepopulate the field

vestal lantern
thorny igloo
#

this is the correct way to use the format parameter:
format=('%Y-%m-%dT%H:%M')

vestal lantern
#

oh ok

thorny igloo
#

thank you for trying to help me!

#

!close

warped mapleBOT
#
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.