英文:
Django: A None DateInput does not pass through form validate. How can I fix it?
问题
I use UpdateView
and created a field called date_updated
. When the user creates, e.g., a Group, there are zero updates. In models.py
I defined:
date_updated = models.DateTimeField(null=True)
Then in forms.py
I created the update form, e.g.,
class UpdateGroupForm(forms.ModelForm):
class Meta:
model = Group
fields = ['name', 'date_updated']
In templates I call my form, as expected in the iteration {% for field in form %}
, field.value = None
for the specific field date_updated
, and because of this I believe is not updating. In views.py
I have the following in the UpdateGroup
class
def form_valid(self, form):
if form.instance.owner == self.request.user:
form.instance.date_updated = timezone.now()
return super().form_valid(form)
else:
return super().form_invalid(form)
I tried to replace the empty value with a fake date considering in my templates the following:
{% if field.name == 'date_updated' and field.value == None %}
<input type="date" name="date_updated" value="2000-01-01" format="yyyy-mm-dd" required disabled>
{% else %}
But it doesn't work also, I cannot validate the form and update my values with the first update.. What can I do? The only thing that works is to remove date_updated
from forms.py
, do the first update and then restore it in forms.py
. Now because is not an empty Date everything works and I can proceed with a second update....so the first update is where I'm stuck
英文:
I use UpdateView
and created a field called date_updated
. When the user creates, e.g., a Group, there are zero updates. In models.py
I defined:
date_updated = models.DateTimeField(null=True)
Then in forms.py
I created the update form, e.g.,
class UpdateGroupForm(forms.ModelForm):
class Meta:
model = Group
fields = ['name','date_updated']
In templates I call my form, as expected in the iteration {% for field in form %}
,
field.value = None
for the specific field date_updated
, and because of this I believe is not updating. In views.py
I have the following in the UpdateGroup
class
def form_valid(self, form):
if form.instance.owner == self.request.user:
form.instance.date_updated = timezone.now()
return super().form_valid(form)
else:
return super().form_invalid(form)
I tried to replace the empty value with a fake date considering in my templates the following:
{% if field.name == 'date_updated' and field.value == None %}
<input type="date" name="date_updated" value="2000-01-01" format="yyyy-mm-dd" required disabled>
{% else %}
But it doesn't work also, I cannot validate the form and update my values with the first update.. What can I do? The only thing that works is to remove date_updated
from forms.py
, do the first update and then restore it in forms.py
. Now because is not an empty Date everything works and I can proceed with a second update....so the first update is where I'm stuck
答案1
得分: 1
你可以从你的表单和模板中排除字段 date_updated
,因为你想要它是 timezone.now()
。这样,你就不需要像 None
那样设置它的值了。你应该保持这部分代码 form.instance.date_updated = timezone.now()
,它会实现你想要的效果。
英文:
You can exclude the field date_updated
from your form and template since you want it to be timezone.now()
. This way you wont have to set a value for it like None. You should just keep this part form.instance.date_updated = timezone.now()
which will do what you wanted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论