django - 显示所有有在场和不在场状态的员工
问题描述
我有两个模型
用户模型和 DailyPresent 模型,用户为 DailyPresent 模型中的 foreign_key。
class DailyPresentReport(models.Model):
PRESENT = 'present'
ABSENT = 'absent'
ON_LEAVE = 'on_leave'
PRESENT_CHOICES = (
(PRESENT, 'Present'),
(ABSENT, 'Absent'),
(ON_LEAVE, 'On Leave'),
)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='daily_present_report')
present = models.CharField(max_length=10, choices=PRESENT_CHOICES, default=ABSENT)
punch_in = models.DateTimeField(null=True, blank=True)
punch_out = models.DateTimeField(null=True, blank=True)
date = models.DateField(null=True, blank=True)
work_time = models.DurationField(null=True, blank=True)
class Meta:
ordering = ['id']
def __str__(self):
return f'{str(self.user)}: {self.present}'
If User logs in then he is automatically made present
但是当用户没有登录时,什么也没有发生。
现在我想显示一个包含所有用户的表格,其中包含存在和不存在字段。
请帮助我..提前谢谢
解决方案
您可以使用 Q 模块,在搜索时处理“或”条件的组合:
from django.db.models.query_utils import Q
dailyPresents = DailyPresentReport.objects.filter(Q(present="PRESENT") |
Q(present="ABSENT"))
或用于.exclude
排除 On Leave:
dailyPresents = DailyPresentReport.objects.exclude(present="ON_LEAVE")
推荐阅读
- python - 如果有 GIL,Python 守护程序线程会被繁忙的父线程阻塞吗?
- go - 点燃缓存配置未反映在 golang 客户端上
- xamarin - Xamarin Essentials 是否有自己的生物识别实施?
- json - JSON / PowerShell - 将 ps 变量存储在 JSON 中,并在转换为 powershell 对象时执行
- javascript - 无法使用 Dialogflow 的 InlineEditer 从 Firebase 设置/获取数据
- javascript - react native 中的 createBottomTabNavigator 有一个小的可点击区域
- php - PHP麻烦json_decoding多级json数组
- mongodb - MongoDB 切片数组并仅检索投影中的该数组字段
- hybris - 自定义属性的 Hybris 同步错误
- python - 有没有办法制作多组?