Can I make the foreign key field optional in Django model
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
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)
The on_delete
argument is necessary and would be better if you do it this way.
subject = models.ForeignKey(subjects, on_delete=models.SET_NULL, blank=True, null=True)