Django在包含天数列表的多对多字段中过滤日期名称

时间:2017-10-19 09:58:50

标签: python django filter many-to-many

class DayOfWeek(models.Model):
    name = models.CharField(max_length=45)


class PermanentShiftSchedule(models.Model):
    id = models.UUIDField(primary_key=True, editable=False, default=uuid4)
    shift = models.ForeignKey(Shift,  related_name='shift_permanent_schedule', on_delete=models.CASCADE)
    branch_employee = models.ForeignKey(BranchEmployee, related_name='branch_emp_permanent_schedule',
                                    on_delete=models.CASCADE)
    days_of_week = models.ManyToManyField(DayOfWeek, related_name='permanent_shift_days')

现在我要过滤PermanentShiftSchedule,其中 days_of_week 字段contanins“星期四”

days_of_week 字段可能包含多天名称

1 个答案:

答案 0 :(得分:1)

您可以尝试按相关使用双下划线__

进行过滤
PermanentShiftSchedule.objects.filter(days_of_week__name__icontains="Thursday")