'Filter related field Django orm shows multiple invalid results
Query
TechnicianAssignment.objects.filter(Q(slot__slot_date=curr_datetime.date())&Q(assigned_technician__attendance_Technician__attendance_status__in=['Rejected', 'Absent', 'Someone else reported', 'Present']))
Models in short:
**Table 1**:
class TechnicianTeam(models.Model):
id = models.AutoField(primary_key=True)
supervisor = models.ForeignKey('self', null=True, blank=True, related_name="TechnicianSupervisor", on_delete=models.DO_NOTHING)
customer_profile = models.ForeignKey('login.CustomerProfile',
related_name="technician_TeamUser", on_delete=models.DO_NOTHING)
class Meta:
db_table = "technician_team"
**Table2:**
class TechnicianAssignment(models.Model):
id = models.AutoField(primary_key=True)
slot = models.ForeignKey('technician_management.TechnicianSlot',
related_name="assignment_Slot", on_delete=models.DO_NOTHING)
assigned_technician = models.ForeignKey('technician_management.TechnicianTeam',
related_name="assignment_Technician", on_delete=models.DO_NOTHING)
class Meta:
db_table = "technician_assignment"
**Table3**
ATTENDANCE_CHOICES = [
('Rejected','Rejected'),
('Someone else reported','Someone else reported'),
('Absent','Absent'),
('Present','Present')
]
class TechnicianAttendance(models.Model):
id = models.AutoField(primary_key=True)
technician = models.ForeignKey('technician_management.TechnicianTeam',
related_name="attendance_Technician", on_delete=models.DO_NOTHING)
slot = models.ForeignKey('technician_management.TechnicianSlot',
related_name="attendance_Slot", on_delete=models.DO_NOTHING)
attendance_status = models.CharField(max_length=30, choices=ATTENDANCE_CHOICES, null=True)
class Meta:
db_table = "technician_attendance"
Question what is wrong with my query:
I need to filter out from TechnicianAssignment where in entries were "slot__slot_date" is current date and "__attendance_Technician__attendance_status__in" = ['Rejected', 'Absent', 'Someone else reported', 'Present']
there is only 1 entry in db with attendance status 'Present' but i am getting many output because of "__attendance_Technician__attendance_status__in" this filter.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
