姜戈。用于表单的多对多字段,但不适用于模型
我的数据库应该有一个类型为多对多的字段,但事实并非如此,我无法更改这一点。
例如,我有一个学生列表和一个科目列表。主题应该是学生表中的多对多字段,但正如我所说,事实并非如此。学生表根本没有这个字段。但还有另一个表 Students-subject 包含 subject_id-student_id 项。
当我将学生保存在数据库中时,如何在学生表单中嵌入某种可以更改学生科目数据的学科字段?问题是我无法更改数据库结构,因此只能在 Django 的帮助下进行更改。
I have DB that should have one field with type Many-To-Many, but it is not and I can't change this.
For example I have a list of students and a list of subjects. Subject should be many-to-many field in students table, but as i said it is not. Students table doesn't have this field at all. But there is still another table students-subjects that contains subject_id-student_id items.
How can I embed in student form some kind of subject field that could change students-subjects data when I save a student in DB? The problem is that I can't change the DB structure so need to make it only with help of Django.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库表中不存在“多对多字段”之类的东西,就像您可能知道的外键一样。在数据库表示中,多对多关系是通过使用额外的表来实现的,该表链接要设置关系的记录的主键(通常是 id)。在您的情况下,这是学生科目表。当您在模型中定义多对多关系时,Django 使用此表。您根本不必更改数据库结构,它将像现在一样完美地工作。
请参阅文档: ManyToManyField
您必须设置db_table 选项带有中间表的名称(即学生科目)。那么一切都应该工作正常。
编辑:
考虑到您的评论,问题是 Django 需要特定的命名约定(即 MODELNAME_id),而您的表未提供该约定。既然你说你不能改变表本身,你必须尝试其他的东西。
您必须为中间表(学生-科目)创建一个额外的模型,并将字段“学生”定义为学生模型的外键,将字段“科目”定义为科目模型的外键。然后,对于多对多字段,您可以使用中间表的名称指定选项“through”。设置选项“db_column”,让 Django 知道您想要为数据库列使用哪些名称。元类中的“db_table”需要指定您的数据库表名称。
您会得到类似这样的信息:
我希望这会对您有所帮助。
有关更多详细信息,请参阅:多对多关系的额外字段。
There is no such thing as a 'Many-to-many field' for a database-table like you might know it of foreign keys. In database representation many-to-many relationships are realized by using an extra table that links the primary keys (usually the ids) of the records you want to set in relation. In your case that is the table students-subjects. Django uses this table when you define a many-to-many relationship in the model. You do not have to change your database structure at all, it will be working perfectly as it is now.
See the documentation: ManyToManyField
You'll have to set the db_table option with the name of your intermediary table (i.e. students-subjects). Then everything should work fine.
EDIT:
Considering your comment, the problem is that Django expects a certain naming convention (i.e. MODELNAME_id) which isn't provided by your table. Since you say that you cannot change the table itself, you have to try something else.
You have to create an extra Model for your intermediary table (students-subjects) and define the field 'students' as a foreign key to the students model and the field 'subjects' as a foreign key to the subjects model. Then for the many-to-many field you specifiy the option 'through' with the name of your intermediary table. Set the options 'db_column' to let Django know which names you'd like to use for the databse columns. 'db_table' in the meta class is needed to specify your database table name.
You get something like:
I hope that will help you.
For more detail see: Extra fields on many-to-many relationship.