我的模型定义如下:
class Subject(models.Model):
subject_id = models.IntegerField(primary_key=True)
subject_name = models.CharField(max_length=20)
class Teacher(models.Model):
teacher_id = models.CharField(max_length=10, primary_key=True)
teacher_name = models.CharField(max_length=30)
teacher_age = models.IntegerField()
teacher_doj = models.DateField()
subjects = models.ManyToManyField(Subject)
class TeacherScores(models.Model):
co_id = models.CharField(primary_key = True, max_length=5)
internal_score = models.IntegerField()
external_score = models.IntegerField()
teacher = models.ManyToManyField(Teacher)
下面显示的数据库条目很少
教师
+------------+--------------+-------------+-------------+
| teacher_id | teacher_name | teacher_age | teacher_doj |
+------------+--------------+-------------+-------------+
| AC23001 | Tina | 32 | 2017-04-10 |
| AC23002 | Rina | 31 | 2009-04-10 |
| AC23003 | Tom | 35 | 2009-04-10 |
| AC23004 | Henry | 56 | 2009-04-10 |
+------------+--------------+-------------+-------------+
受试者
+------------+--------------+
| subject_id | subject_name |
+------------+--------------+
| 1 | English |
| 2 | Hindi |
| 3 | Sanskrit |
| 4 | Math |
| 5 | Physics |
| 6 | Chemistry |
| 7 | Biology |
| 8 | History |
| 9 | Civics |
| 10 | Geography |
| 11 | MoralScience |
| 12 | Algebra |
+------------+--------------+
teacher_subject
+----+------------+------------+
| id | teacher_id | subject_id |
+----+------------+------------+
| 4 | AC23002 | 4 |
| 5 | AC23002 | 8 |
| 2 | AC23002 | 12 |
| 6 | AC23003 | 5 |
| 7 | AC23003 | 9 |
| 10 | AC23004 | 5 |
| 8 | AC23005 | 9 |
| 9 | AC23005 | 10 |
| 11 | AC23006 | 12 |
| 12 | AC23007 | 6 |
| 13 | AC23008 | 6 |
+----+------------+------------+
基本上我想为他/她处理的每个主题的特定教师添加教师模型的条目。 在这里,我想修改TeacherScores模型,以便我可以为每个主题的教师包含分数。 我尝试通过teachercores对象插入主题,但是我收到了错误。
>>> t1 = TeacherScores('C006','23','24',teacher__subject='Math')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/Django-1.11.5-py2.7.egg/django/db/models/base.py", line 572, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'teacher__subject' is an invalid keyword argument for this function
>>> t1 = TeacherScores('C006','23','24',teacher__subjects='Math')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/Django-1.11.5-py2.7.egg/django/db/models/base.py", line 572, in __init__
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'teacher__subjects' is an invalid keyword argument for this function
如果在教师和主题之间定义了ManyToMany字段,还有TeacherScores和Teacher <,如何更改我的模型 - TeacherScores 以包含教师的主题字段 / p>
答案 0 :(得分:1)
您要做的是创建用于对TeacherSubject
和Teacher
之间的M2M关系建模的显式Subject
类。然后,您可以在TeacherScores
中引用它。
Teacher
选项模型M2M Subject
- through
关系:class TeacherSubject(models.Model):
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
# extra fields ...
然后修改Teacher
和TeacherScores
以使用TeacherSubject
:
# Teacher:
subjects = models.ManyToManyField(Subject, through=TeacherSubject)
# TeacherScores:
teachersubject = models.ForeignKey(TeacherSubject)
TeacherSubject
:t1 = TeacherScores('C006','23','24', teachersubject=some_teachersubject_instance)
详细说明here。