我有三种模式:客户,合同,报告
class Customer
pass
class Contract
customer = models.ForeignKey(Customer)
class Report
customer = models.ForeignKey(Customer)
contract = models.ForeignKey(Contract, null=True, blank=True, default=None)
在管理员编辑报告时我想选择客户或合同,如果我选择合同客户必须设置为合同的客户
答案 0 :(得分:0)
在 admin.py 覆盖保存方法中
def save_model(self, request, obj, form, change):
if obj.contract:
obj.customer = obj.contract.customer # set customer to related contract customer
else:
#probably you do not do anything in here
obj.save()