1👍
✅
When you create you B object you need to provide a reference to an A object.
You say you’re using a form to create the B object so you’ll probably need something along the lines of…
b_instance = new_b_form.save(commit=False)
b_instance.a = A.objects.get(something="whatever") # You'll know how to find the A object you want related.
b_instance.save()
Setting a will automatically set a_id to be saved in the database.
Alternatively you may be able to change your form so that the user can select the A object.
1👍
You should allow the model B FK field to be empty (null=True, blank=True) if you want to save it without reference to model A i.e. if FK is Null.
Otherwise you will have to feed the B object with A object and then to save it.
Source:stackexchange.com