0

我有三个模型:客户、合同、报告

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)

在管理员中编辑报告时我想选择客户或合同,如果我选择合同客户必须设置为合同的客户

4

1 回答 1

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()
于 2013-01-26T12:47:04.203 回答