如何在Django-fi中测试列表中的一个元素是否在另一个列表中

2024-10-04 09:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用Django过滤器使这个for查询工作。有什么帮助吗

tmp = TemporaryLesson.objects.filter(Q(expiration_date__gte=now()) | Q(expiration_date__isnull=True))
temporary_lessons = []
for t in tmp:  # How to make this manual query works in the filter above?
    for c in t.related_courses:
        if c in student.my_courses:
            temporary_lessons.append(t)
            break

编辑: 模型变量

来自学生

my_courses = models.ManyToManyField(
    'course.BaseCourse', 
    related_name='students', 
    through=CourseXStudent
)

从临时课程

related_courses = models.ManyToManyField(
    'course.BaseCourse',
    related_name='temporary_lessons'
)

Tags: infordatemodelsmyfiltertmprelated