[Answer]-OneToOneField Relationship Not Created

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

Leave a comment