Django - 获取最近 7 天的数据库记录
Django - Get database records for last 7 days
我正在尝试从我的 SQLite 数据库中检索最近 7 天的记录。我有一个出勤系统,我正在尝试在 UI 上显示一些统计信息,但目前它无法正常工作 - 我将在下面显示我当前的代码。
型号:
class Meta:
model = User
fields = ("username", 'email', 'password1', 'password2')
class is_Present(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(default=datetime.date.today)
is_present = models.BooleanField(default=False)
class clocked_Time(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(default=datetime.date.today)
time = models.DateTimeField(null=True, blank=True)
signed_out = models.BooleanField(default=False)
Views.py:
# Get Count of Present Employees in last week
def present_week_employees():
today = date.today()
week = today - timedelta(days=7)
present_employees_all = is_Present.objects.filter(date=week).filter(is_present=True)
return len(present_employees_all)
# Displays admin attendance portal functions
def attendance_portal(request):
if not request.user.is_authenticated:
messages.warning(request, f'Please sign in to mark attendance out')
return redirect('login')
elif not request.user.is_superuser or not request.user.is_staff:
messages.warning(request, f'Must be admin to access this feature')
return redirect('home')
elif request.user.is_superuser:
count_employees_all = count_employees() # shows count of employees
present_employee_all = present_employees() # shows count present employees today
present_employee_all_week = present_week_employees() # shows count present employees in last 7 days
# Gets the employees present today/week
today = datetime.today()
week = today - timedelta(days=7)
# Gets employees displayed and paginated
today_p = Paginator(is_Present.objects.filter(date=today).filter(is_present=True).select_related('user').all(),5)
x = Paginator(is_Present.objects.filter(date=week).filter(is_present=True).select_related('user').all(), 5)
page = request.GET.get('page', 1)
try:
users = today_p.get_page(page) # returns the desired page object
except PageNotAnInteger:
# if page_number is not an integer then assign the first page
users = today_p.page(1)
except EmptyPage:
# if page is empty then return last page
users = today_p.page(today_p.num_pages)
# this_week_emp_count_vs_date()
# last_week_emp_count_vs_date()
try:
all_users = x.get_page(page)
except PageNotAnInteger:
# if page_number is not an integer then assign the first page
all_users = x.page(1)
except EmptyPage:
# if page is empty then return last page
all_users = x.page(x.num_pages)
return render(request, "users/adminReports.html",
{'count_employees_all': count_employees_all, 'present_employee_all': present_employee_all,
'present_employee_all_week': present_employee_all_week, 'all_users': all_users, 'users': users})
else:
messages.warning(request, f'Error - please see logs for details.')
return redirect(request, 'home')
HTML:
<div class="card" id="emp_present_week">
<div class="card-body">
<h5 class="card-title"> <b> Employees present in last 7 days</b></h5>
<p class="card-text" style="padding-top: 1em; font-size: 28px;"><b> {{ present_employee_all_week }}</b></p>
</div>
</div>
另外,我附上一张图片以供参考。仅供参考 - 当我将用户标记为 today 时,它会在今天和 7 天内显示他们,这是正确的。但是,我昨天标记为在场的员工今天没有显示在 7 天计数中。我认为潜在的问题可能出在以下地方:
present_employees_all = is_Present.objects.filter(date=week).filter(is_present=True)
可能我需要循环过去 7 天?这个方法我试过了,没成功。
如有任何帮助,我们将不胜感激!
Image of the system statistics
你的查询是错误的,等于意味着你只获得了 7 天前的记录。您需要查询从过去 7 天到现在,请在查询中使用 __gte(gte 大于或等于)
应该是这样的:
today = date.today()
seven_day_before = today - timedelta(days=7)
present_employees_all = is_Present.objects.filter(date__gte=seven_day_before, is_present=True)
也不需要使用链式过滤器,过滤器支持多个AND条件
我正在尝试从我的 SQLite 数据库中检索最近 7 天的记录。我有一个出勤系统,我正在尝试在 UI 上显示一些统计信息,但目前它无法正常工作 - 我将在下面显示我当前的代码。
型号:
class Meta:
model = User
fields = ("username", 'email', 'password1', 'password2')
class is_Present(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(default=datetime.date.today)
is_present = models.BooleanField(default=False)
class clocked_Time(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.DateField(default=datetime.date.today)
time = models.DateTimeField(null=True, blank=True)
signed_out = models.BooleanField(default=False)
Views.py:
# Get Count of Present Employees in last week
def present_week_employees():
today = date.today()
week = today - timedelta(days=7)
present_employees_all = is_Present.objects.filter(date=week).filter(is_present=True)
return len(present_employees_all)
# Displays admin attendance portal functions
def attendance_portal(request):
if not request.user.is_authenticated:
messages.warning(request, f'Please sign in to mark attendance out')
return redirect('login')
elif not request.user.is_superuser or not request.user.is_staff:
messages.warning(request, f'Must be admin to access this feature')
return redirect('home')
elif request.user.is_superuser:
count_employees_all = count_employees() # shows count of employees
present_employee_all = present_employees() # shows count present employees today
present_employee_all_week = present_week_employees() # shows count present employees in last 7 days
# Gets the employees present today/week
today = datetime.today()
week = today - timedelta(days=7)
# Gets employees displayed and paginated
today_p = Paginator(is_Present.objects.filter(date=today).filter(is_present=True).select_related('user').all(),5)
x = Paginator(is_Present.objects.filter(date=week).filter(is_present=True).select_related('user').all(), 5)
page = request.GET.get('page', 1)
try:
users = today_p.get_page(page) # returns the desired page object
except PageNotAnInteger:
# if page_number is not an integer then assign the first page
users = today_p.page(1)
except EmptyPage:
# if page is empty then return last page
users = today_p.page(today_p.num_pages)
# this_week_emp_count_vs_date()
# last_week_emp_count_vs_date()
try:
all_users = x.get_page(page)
except PageNotAnInteger:
# if page_number is not an integer then assign the first page
all_users = x.page(1)
except EmptyPage:
# if page is empty then return last page
all_users = x.page(x.num_pages)
return render(request, "users/adminReports.html",
{'count_employees_all': count_employees_all, 'present_employee_all': present_employee_all,
'present_employee_all_week': present_employee_all_week, 'all_users': all_users, 'users': users})
else:
messages.warning(request, f'Error - please see logs for details.')
return redirect(request, 'home')
HTML:
<div class="card" id="emp_present_week">
<div class="card-body">
<h5 class="card-title"> <b> Employees present in last 7 days</b></h5>
<p class="card-text" style="padding-top: 1em; font-size: 28px;"><b> {{ present_employee_all_week }}</b></p>
</div>
</div>
另外,我附上一张图片以供参考。仅供参考 - 当我将用户标记为 today 时,它会在今天和 7 天内显示他们,这是正确的。但是,我昨天标记为在场的员工今天没有显示在 7 天计数中。我认为潜在的问题可能出在以下地方:
present_employees_all = is_Present.objects.filter(date=week).filter(is_present=True)
可能我需要循环过去 7 天?这个方法我试过了,没成功。
如有任何帮助,我们将不胜感激!
Image of the system statistics
你的查询是错误的,等于意味着你只获得了 7 天前的记录。您需要查询从过去 7 天到现在,请在查询中使用 __gte(gte 大于或等于)
应该是这样的:
today = date.today()
seven_day_before = today - timedelta(days=7)
present_employees_all = is_Present.objects.filter(date__gte=seven_day_before, is_present=True)
也不需要使用链式过滤器,过滤器支持多个AND条件