Django: None DateInput 不会通过表单验证。我该如何修复它?

huangapple go评论103阅读模式
英文:

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 = [&#39;name&#39;,&#39;date_updated&#39;]

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 == &#39;date_updated&#39; and field.value == None %}
 &lt;input type=&quot;date&quot; name=&quot;date_updated&quot; value=&quot;2000-01-01&quot; format=&quot;yyyy-mm-dd&quot; required disabled&gt;                      
{% 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.

huangapple
  • 本文由 发表于 2023年2月14日 22:16:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定