显示所有具有出席和缺席状态的员工
Display all Employees with present and absent status
我有两个模型
用户模型和 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
但是当用户没有登录时没有任何反应。
现在我想显示一个 table 包含所有用户的存在和缺席字段。
请帮助我..提前致谢
可以使用Q模块,搜索时处理“或”条件的组合:
from django.db.models.query_utils import Q
dailyPresents = DailyPresentReport.objects.filter(Q(present="PRESENT") |
Q(present="ABSENT"))
或使用.exclude
排除休假:
dailyPresents = DailyPresentReport.objects.exclude(present="ON_LEAVE")
我有两个模型
用户模型和 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
但是当用户没有登录时没有任何反应。
现在我想显示一个 table 包含所有用户的存在和缺席字段。
请帮助我..提前致谢
可以使用Q模块,搜索时处理“或”条件的组合:
from django.db.models.query_utils import Q
dailyPresents = DailyPresentReport.objects.filter(Q(present="PRESENT") |
Q(present="ABSENT"))
或使用.exclude
排除休假:
dailyPresents = DailyPresentReport.objects.exclude(present="ON_LEAVE")