def Meta(self):
    unique_together = 'key', 'fk'

def save(self, *args, **kwargs):
    key = cal_key(self.fk)
self.key = key
super(ModelA, self).save(*args, **kwargs)
Django Model Auto Increment Primary Key Based on Foreign Key