Display all Employees with present and absent status

155 Views Asked by At

I Have Two models

User Model and DailyPresent model with user as foreign_key in DailyPresent model.

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 

but when user doesn't login nothing happens.

Now I want to display a table with all users with Present and Absent Fields.

Please help me.. Thanks in advance

1

There are 1 best solutions below

1
On BEST ANSWER

you can use Q module, processing the combination of "or" conditions when searching:

from django.db.models.query_utils import Q

dailyPresents = DailyPresentReport.objects.filter(Q(present="PRESENT") |
                                                  Q(present="ABSENT"))

or use .exclude to exclude the On Leave:

dailyPresents = DailyPresentReport.objects.exclude(present="ON_LEAVE")