Hello I am trying to create an employee attendance program and I want to make it so that the employees can see the total hours they worked.
This is the code that creating the problem:
elif 'checkName' in response.POST:
if form.is_valid():
n = form.cleaned_data["name"]
t = Name.objects.filter(name=n)
totalHours = datetime.combine(t.date, t.timeOut) - datetime.combine(t.date, t.timeIn)
messages.success(response, totalHours)
return redirect('/')
The error I get is
'QuerySet' object has no attribute 'date'
and if I use
t = Name.objects.get(name=n)
it shows an error that says
'get() returned more than one Name -- it returned 2!'
Advertisement
Answer
You must use filter and iterate over results as get is used to fetch exactly one record. Try this:
...
names = Name.objects.filter(name=n)
totalHours = 0
for name in names:
totalHours += (
datetime.combine(name.date, name.timeOut)
- datetime.combine(name.date, name.timeIn)).seconds / 3600
...