I have django app that is tracking contracts, locations, and products for various clients. Contracts, Locations each reference the client with a ForeignKey.
class Location(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
...
class Contract(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
...
I also have a product that will be deployed in a location, on a contract, for a client. I need to include client in product because the reference is used to secure the record to that client.
class Product(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE)
contract = models.ForeignKey(Contract, on_delete=models.CASCADE)
location = models.ForeignKey(Location, on_delete=models.CASCADE)
....
What is the best way to make sure a product is never created that contains a different client reference across all 3 models? I was thinking I could use a pre_save signal but I would rather use a database constraint but I don't see a database constraint to force this.