Model:
class Comment(models.Model):
date = models.DateTimeField(default = datetime.datetime.now)
text = models.TextField()
Form:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
In template I have only “text” field. When I click on submit button, QueryDict in form.data contains only text, despite the default value for date field. How to set default value properly?
Update:
I figured that
default=datetime.date.nowbehaved likeauto_now_add=Truebecause you mentioned that your template only contains thetextfield.It turns out your situation is not handled any differently and you must display the
datefield in your form.If you want to ignore it, you can override the
ModelForm.savemethod to set the date yourself, or add it in your modelsave()definition.You should look into the
auto_now_add=Trueparameter in model field definitions which automatically adds this behavior of setting a date field todatetime.datetime.now()upon the first save.