如何在 djangorestframework 中使用 ManytoManyField
我正在使用 djangorestframework。该模型一次会议可以有许多人参加。 所以我尝试使用 django 的 ManytoManyField:
class Meeting(models.Model):
name=models.CharField(max_length=100)
participates=models.ManyToManyField(Person)
通过 python manager.py
sqlall XXX 数据库表是:
CREATE TABLE "OA_meeting_participates" (
"id" integer NOT NULL PRIMARY KEY,
"meeting_id" integer NOT NULL,
"person_id" integer NOT NULL REFERENCES "OA_person" ("id"),
UNIQUE ("meeting_id", "person_id")
)
;
CREATE TABLE "OA_meeting" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(100) NOT NULL,
)
问题来了:当我通过选择一些人创建会议时,服务器响应 'participates'
是该函数的无效关键字参数。 因为 OA_meeting 没有参加字段。 那么如何解决呢?
i'm using djangorestframework. the models one meeting could have many participates.
So i tried to use ManytoManyField of django:
class Meeting(models.Model):
name=models.CharField(max_length=100)
participates=models.ManyToManyField(Person)
by python manager.py
sqlall XXX the DB tables are:
CREATE TABLE "OA_meeting_participates" (
"id" integer NOT NULL PRIMARY KEY,
"meeting_id" integer NOT NULL,
"person_id" integer NOT NULL REFERENCES "OA_person" ("id"),
UNIQUE ("meeting_id", "person_id")
)
;
CREATE TABLE "OA_meeting" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(100) NOT NULL,
)
the problems comes: when i create a meeting by selecting some persons, server response 'participates'
is an invalid keyword argument for this function.
since OA_meeting does not have participates field.
so how to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅许多字段。您需要首先分别创建会议和人员对象,然后执行
meeting.partcipates.add(person)
(顺便说一句,我认为您正在寻找的词是participANts)
see the docs on manytomany fields. You need to create the meeting and person objects separately first, and then do
meeting.partcipates.add(person)
(btw, i think the word you are looking for is participANts)