Home > Blockchain >  I have written a django query but need specific user information of particular date
I have written a django query but need specific user information of particular date

Time:11-28

Models:

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, default=1,related_name='Employee')
    eid = models.IntegerField(primary_key=True)
    salary = models.IntegerField(null=True, blank=True)
    gender = models.CharField(max_length=6, choices=GENDER_CHOICES, default=1)
    contactno = models.CharField(max_length=10, blank=False)
    email = models.CharField(max_length=50 ,null=True, blank=True)
    country = models.CharField(max_length=30)
    address = models.CharField(max_length=60)

    def __str__(self):
        return self.user.first_name    '_'    self.user.last_name


class Attendance(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, default=1,related_name='Attendance')
    attendance_date = models.DateField(null=True)
    in_time = models.TimeField(null=True)
    out_time = models.TimeField(null=True ,blank=True)
    description = models.TextField(null=True, blank=True)

    def __str__(self):
        return str(self.employee)   '-'    str(self.attendance_date)


class Breaks(models.Model):
    employee = models.ForeignKey(Employee, on_delete=models.CASCADE, default=1)
    break_in = models.TimeField(null=True, blank=True)
    break_out = models.TimeField(null=True, blank=True)
    attendance =models.ForeignKey(Attendance, on_delete=models.CASCADE, default=1,related_name='Breaks')
    
    def __str__(self):
        return str(self.employee)   '-'   str(self.break_in)   '-'   str(self.break_out)

def detail_attendance(request):
    attendance_list = Attendance.objects.filter(employee__user_id=request.user.id)
    counter = Counter()
    return render(request, 'employee/detail_attendance.html', {'attendance_list': attendance_list, 'counter': counter})



def detail_break(request):
    break_list=Breaks.objects.filter(employee__user_id=request.user.id )
    return render(request, 'employee/detail_break.html', {'break_list': break_list})

I have created a function above for detail breaks. I am getting specific user data, but it is giving me the previous data as well. So I need the data for specific date for example in my attendance models I adding attendance of each user.

Please let me know what should I change in detail break.

CodePudding user response:

Use this Queryset:

from django.db.models import Q
from datetime import date


Breaks.objects.filter(
    Q(employee__user=request.user) & 
    Q(attendance__attendance_date=date.today())
)

Or:


Breaks.objects.filter(
    Q(employee__user=request.user) & 
    Q(attendance__attendance_date="2022-11-28")
)

CodePudding user response:

Breaks.objects.filter(date__range=["2011-01-01", "2011-01-31"])

Or if you are just trying to filter month wise:

Breaks.objects.filter(date__year='2011', 
                  date__month='01')

Please reply to this message ,If it doesn't work.

  • Related