Reputation: 55
I trying to submit form while keeping field for datetime as empty which results in an error showing invalid format for datetime.
my modal
class lead(models.Model):
assigned_to=models.CharField(max_length=15)
ref=models.CharField(max_length=10,blank=True)
lead_type=models.CharField(max_length=20)
priority=models.CharField(max_length=20)
hot=models.CharField(max_length=20,blank=True)
source=models.CharField(max_length=20,blank=True)
mls_lead=models.CharField(max_length=20,blank=True)
auto_imported=models.CharField(max_length=20,blank=True)
inquiry_date=models.DateTimeField(blank=True,null=True)
lead_closed_date=models.DateTimeField(blank=True,null=True)
created_by=models.CharField(max_length=20,blank=True)
property_id=models.IntegerField(blank=True,null=True)
contact_id=models.IntegerField(blank=True,null=True)
its clear that i have given null true for the datetime field and the form i have is my form
class leads_form(forms.Form):
assigned_to = forms.ChoiceField(choices=[(o.id, str(o.username)) for o in CustomUser.objects.all()],required=True,widget=forms.Select(attrs={'class':'form-control'}))
ref = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
lead_type = forms.ChoiceField(choices=dicto.lead_type_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
priority = forms.ChoiceField(choices=dicto.priority_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
hot = forms.ChoiceField(choices=dicto.hot_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
source = forms.ChoiceField(choices=dicto.source_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
mls_lead = forms.ChoiceField(choices=dicto.true_false_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct'}))
auto_imported = forms.ChoiceField(choices=dicto.true_false_dict.items(),required=False,widget=forms.Select(attrs={'class':'form-control slct'}))
inquiry_date = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
lead_closed_date = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
created_by = forms.CharField(max_length=50,required=False,widget=forms.TextInput(attrs={'class':'form-control'}))
property_id = forms.IntegerField(required=False,widget=forms.HiddenInput())
set_status = forms.ChoiceField(choices=dicto.set_status_dict.items(),required=True,widget=forms.Select(attrs={'class':'form-control slct','placeholder':'set status','required':'true'}))
contact=forms.IntegerField(required=True,widget=forms.HiddenInput(attrs={'required':'true'}))
and when i try to create object of model with validate form data it shows
django.core.exceptions.ValidationError: ["'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
the way i create object is as follows
if form.is_valid():
lead.objects.create(
assigned_to=form.cleaned_data['assigned_to'],
ref=form.cleaned_data['ref'],
lead_type=form.cleaned_data['lead_type'],
priority=form.cleaned_data['priority'],
hot=form.cleaned_data['hot'],
source=form.cleaned_data['source'],
mls_lead=form.cleaned_data['mls_lead'],
auto_imported=form.cleaned_data['auto_imported'],
inquiry_date=form.cleaned_data['inquiry_date'],
lead_closed_date=form.cleaned_data['lead_closed_date'],
created_by=form.cleaned_data['created_by'],
property_id=form.cleaned_data['property_id'],
contact_id=form.cleaned_data['contact'])
Upvotes: 2
Views: 1198
Reputation: 1745
To Django's DateTimeField
, a blank string is not the same as None
(or null). I believe this confusion comes from the similar evaluation of truth values for a blank string and None
.
To assert no error is raised and the field is populated with a null value when wanted, rework DateTimeField
populating lines like this one (from the lead.objects.create
call):
lead_closed_date=form.cleaned_data['lead_closed_date']
to this:
lead_closed_date=(
form.cleaned_data['lead_closed_date']
if form.cleaned_data['lead_closed_date']
else None
)
With this correction, it will populate the value of lead_closed_date
with form.cleaned_date['lead_closed_date']
if the truth value of form.cleaned_data['lead_closed_date']
is True
.
Upvotes: 1