我可以在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屋!
查看全文