Hi again out there,
if you feel like me spamming this forum the last days, then we feel the same
Right now i am encountering a strange problem occuring in a part of my project that was considered “done” since weeks…
In processing a ModelForm in Django template, i get an unsupported operand type(s) for -: 'datetime.date' and 'NoneType'
error, but last time i tried, it was working ???
Specifically i talk about this:
Models.py:
class ForeignMissions(models.Model):
mission = models.CharField(max_length=50)
location = models.CharField(max_length=100)
position = models.CharField(max_length=100, null=True, blank=True)
begin = models.DateField(null=True, blank=True)
end = models.DateField(null=True, blank=True)
employee = models.ForeignKey(Employees, on_delete=models.CASCADE)
objects = MissionDurationManager()
def __str__(self):
return self.mission
def mission_duration(self):
duration = self.end - self.begin
return duration.days
forms.py:
class ForeignMissionsForm(ModelForm):
class Meta:
model = ForeignMissions
fields = '__all__'
employee = forms.ModelChoiceField(queryset=Employees.objects.all(), widget=forms.HiddenInput)
mission = forms.ModelChoiceField(queryset=Mandat.objects.all(), label='',
empty_label="Mandat", required=False, widget=forms.Select(attrs={
'class': "form-select fw-bold border border-1 border-black m-1",
'style': 'max-width: auto;',
'required': 'True',
}))
location = forms.ModelChoiceField(queryset=Einsatzort.objects.all(), label='',
empty_label="Einsatzort", required=False, widget=forms.Select(attrs={
'class': "form-select fw-bold border border-1 border-black m-1",
'style': 'max-width: auto;',
'required': 'True',
}))
position = forms.ModelChoiceField(queryset=Verwendung.objects.all(), label='',
empty_label="Einsatzverwendung", required=False, widget=forms.Select(attrs={
'class': "form-select fw-bold border border-1 border-black m-1",
'style': 'max-width: auto;',
'required': 'True',
}))
begin = forms.DateField(label='', required=False, widget=forms.DateInput(attrs={
'class': "form-control fw-bold border border-1 border-black m-1",
'style': 'max-width: auto;',
'placeholder': 'Beginn',
'title': 'Beginn',
'type': 'text',
'onfocus': "(this.type = 'date')",
'required': 'True',
}))
end = forms.DateField(label='', required=False, widget=forms.DateInput(attrs={
'class': "form-control fw-bold border border-1 border-black m-1",
'style': 'max-width: auto;',
'placeholder': 'Ende',
'title': 'Ende',
'type': 'text',
'onfocus': "(this.type = 'date')",
'required': 'True',
}))
managers.py:
class MissionDurationManager(models.Manager):
def with_duration_days(self):
return self.annotate(
duration_days=ExpressionWrapper(
(F('end') - F('begin')), output_field=fields.DurationField()
)
)
def total_duration_days(self, employee):
return self.with_duration_days().filter(employee=employee).aggregate(
total_duration=Sum('duration_days')
)['total_duration']
views.py:
if 'save_mission' in request.POST:
form = ForeignMissionsForm(request.POST)
if form.is_valid():
form.save()
context['success'] = 'new entry saved'
else:
context['error'] = 'there where errors'
template part for the form is simply {{ form.as_div }}
The error is raised, because of there is no value passed to database for the begin
field and therefore the line duration = self.end - self.begin
raises an error, because calculation with “None” is a bit difficult ;-). But I can not understand, why???