When creating a new record it fails at the form.is_valid(): bcuz of DateTimeField(auto_now_add=True). I do
not know how to deal with this. Cannot find anything in Djangoproject or the forum.
- This field is required.
models.py (partial)
class ToLog(models.Model)
date_time = models.DateTimeField(auto_now_add=True)
views.py (partial)
def create_turn(request):
if request.method == "POST":
form = ToLogForm(request.POST)
if form.is_valid():
tologs = ToLog.objects.create(
social_title= form.cleaned_data.get('social_title'),
)
form.save()
forms.py (partial)
class ToLogForm(forms.ModelForm):
date_time = forms.DateTimeField()
completed = forms.BooleanField(required=False) # widget = forms.CheckboxInput(attrs={'class': 'form-control'})
entrytype = forms.ChoiceField(choices=[[1, 'Control_M'],[2, 'SolarWinds'],[3, 'FYI'],[4, 'Other'],[5, 'Adhoc']],
widget = forms.Select(attrs={'class': 'form-control'})) #label = "Entrytype") # choices=EntryType.choices, blank=True, null=True)
#validation
def clean(self):
super(ToLogForm, self).clean()
social_title = self.cleaned_data.get('social_title')
if len(social_title)<4:
self.add_error('title','Can not save social_title less than 4 characters long')
self.fields['social_title'].widget.attrs.update({'class': 'form-control is-invalid'})
return self.cleaned_data
class Meta:
model = ToLog
fields = '__all__'