我可以在 Django 模型中使外键字段可选吗 [英] Can I make the foreign key field optional in Django model
本文介绍了我可以在 Django 模型中使外键字段可选吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码
subject = models.ForeignKey(subjects)
location = models.ForeignKey(location)
publisher = models.ForeignKey(publisher)
我对书的三个价值并不总是可能的.所以有时如果我不知道主题或位置或出版商.然后我想让它们空着
There its not always possible that I have three values of books. so sometimes if I don't know subject or location, or publisher. Then I want to keep them empty
但是如果我有那么我需要选择框来选择.有可能吗
But if I have then I need select box to select. is it possible like that
推荐答案
当然,只需为每个你想保持可选的字段添加 blank=True, null=True
就像
Sure, just add blank=True, null=True
for each field that you want to remain optional like
subject = models.ForeignKey(subjects, blank=True, null=True)
这篇关于我可以在 Django 模型中使外键字段可选吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文