1π
β
In order to save a relationship, the object needs to have a primary key; and this is only generated after the object is saved.
Therefore, you need to save the object first, before assigning it as a foreign key:
home_location.save()
self.instance.customer.home_location = home_location
# home_location.save() - this line should come before any relationships
# are linked to the object.
self.instance.customer.save()
π€Burhan Khalid
Source:stackexchange.com