Can I make the foreign key field optional in Djang

2019-01-13 01:20发布

I have this code

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

1条回答
混吃等死
2楼-- · 2019-01-13 01:47

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)
查看更多
登录 后发表回答