You would have to add blank=True as well in field definition. If the model field has blank=True, then required is set to False on the form field. Otherwise, required=True. Don't forget to reset and sync DB again after changing this.
django rest framework - ForeignKey does not allow null values - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
The simplest way is by using the field option blank=True (docs.djangoproject.com/en/dev/ref/models/fields/#blank).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With