django 从两个表中形成引用完整性

发布于 2024-09-02 07:56:38 字数 315 浏览 6 评论 0原文

我有一个名为cv的类,和一个名为大学的类,每个完成简历的用户都应该选择他就读的大学。

我的问题是:一个学生可以在一所或两三所大学学习,也可能是非学生的用户。

我需要将这些数据放入表单中,我使用 ModelForm。来自Cv类的数据和来自University类的数据采用相同的形式,并且用户可以添加一所或多所大学,或者不添加大学。 (同形式)

我该怎么办?我应该使用 ModelForm 吗?如果我在简历类中有外键,并且用户不是学生(所以他在零大学),我可能会收到引用完整性错误。

多谢

i have a class named cv,and a class named university, and each user that completes his cv, should choose a University he studyes at.

My problem is: one student can study at one or 2 or three universities, or may be a user that is not student.

I need to take this data into a form, and i use ModelForm. The data from the Cv class, and from the University class in the same form, and the user can add one or more universities, or no university. (in the same form)

How should i do it? Should i use ModelForm? if i have a foreign key in the CV class, and the user is not a student (so he is at zero universities), i may get an referencial integrity error.

thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长发绾君心 2024-09-09 07:56:38

ModelForms 会将您的 ManyToManyField(这是 Cv 类中 university 字段的正确类型)显示为多个- 选择小部件。

如果您更喜欢复选框,请在表单类中为该字段使用 forms.CheckboxSelectMultiple 小部件。

ModelForms will display your ManyToManyFields (that's the correct type for your university field in your Cv class) as multipe-select widget.

If you prefer checkboxes, use the forms.CheckboxSelectMultiple widget for this field in your form class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文