【Task】与【Student】是多对多关系,为Task的member属性赋值
In [62]: t = Task(title='task add student')
In [65]: t.save()
In [66]: t.member.add(Student.objects.get(name='张晓宇'))
In [67]: t.member.add(Student.objects.get(name='邢镇宇'))
In [68]: t.member.all()
Out[68]: <QuerySet [<Student: 1 1517063108 网络162 >, <Student: 2 1517063216 计
应152 >]>
In [69]: t.member.count()
Out[69]: 2
过滤出今年创建的【任务】所涉及到的【学生】
In [71]: Student.objects.filter(task__create_date__year=current_year)
Out[71]: <QuerySet [<Student: 1 1517063108 网络162 >, <Student: 2 1517063216 计
应152 >, <Student: 1 1517063108 网络162 >, <Student: 2 1517063216 计应152 >, <St
udent: 1 1517063108 网络162 >, <Student: 2 1517063216 计应152 >, <Student: 1 151
7063108 网络162 >, <Student: 2 1517063216 计应152 >, <Student: 1 1517063108 网络
162 >, <Student: 2 1517063216 计应152 >, <Student: 1 1517063108 网络162 >, <Stud
ent: 2 1517063216 计应152 >, <Student: 1 1517063108 网络162 >, <Student: 2 15170
63216 计应152 >, <Student: 1 1517063108 网络162 >, <Student: 2 1517063216 计应15
2 >, <Student: 1 1517063108 网络162 >, <Student: 2 1517063216 计应152 >, <Studen
t: 1 1517063108 网络162 >, <Student: 2 1517063216 计应152 >, '...(remaining elem
ents truncated)...']>
删除上面过滤出来的学生
In [72]: s = Student.objects.filter(task__create_date__year=current_year)
In [73]: s.delete()
Out[73]:
(26,
{u'task_tool.Student': 2,
u'task_tool.TaskReport': 0,
u'task_tool.Task_member': 24})