前检查的关系我的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