前檢查的關係我的Django模型由兩個類annualReport
和annualReportAttachment
的Django模型:保存對象
兩個模型之間的關係是oneToMany
。在管理形式,我需要驗證用戶已上傳至少一個文件,所以我用下面的清潔方法在annualReport
類
def clean(self):
attachments = annualReportAttachment.objects.filter(annualReport=self)
if len(attachments) == 0:
raise ValidationError("You should upload at least one file")
的問題是,附加的文件尚未保存,因此attachments
變量是空的,表單總是會引起錯誤。
如何檢查用戶是否上傳了至少一個文件?
中的RequireOneFormSet類,如果len(附件)> 0: – catherine 2013-03-05 14:37:26
AnnualReportAttachment在您的admin類的annualReport中表示爲內聯模型嗎? – Brandon 2013-03-05 14:40:05
@Brandon是的,它是一個內聯模型 – 2013-03-05 14:41:55