django - ModelAdmin预先填充的外键

时间:2013-01-26 12:02:57

标签: django django-forms django-admin

我有三种模式:客户,合同,报告

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)

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

1 个答案:

答案 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()
相关问题