Mirage
Mirage

Reputation: 31568

How can i override the value of form submitted in django

I am saving the form in the view like this

form.save()

but before that i want to set the datetime field to now.

something like

form.timestamp = datetime.now

how can i do that

Upvotes: 1

Views: 75

Answers (2)

r_31415
r_31415

Reputation: 8982

Better way:

Change the field in your models.py using auto_now_add and auto_now:

date = models.DateTimeField(auto_now_add=True, auto_now=True)

In this way, every time you save your form, your field gets updated.

Documentation here.

Upvotes: 3

jondykeman
jondykeman

Reputation: 6272

instance = form.save(commit=False)
instance.timestamp = datetime.now
instance.save()

JD

Upvotes: 0

Related Questions