Question

I have django model consists of two class annualReport and annualReportAttachment

The relation between the two models is oneToMany. In the admin form I need to validation that the user has uploaded at least one file so I use the following clean method in the annualReport class

def clean(self):
    attachments = annualReportAttachment.objects.filter(annualReport=self)
    if len(attachments) == 0:
        raise ValidationError("You should upload at least one file")

The problem is that the attached files is not saved yet so the attachments variable is empty and the form always raise that error.

How could I check that the user has uploaded at least one file?

Was it helpful?

Solution

You'll need to make sure that at least one form in your inline model gets saved. To do this, I would recommend leveraging the RequireOneFormSet class from https://code.google.com/p/wadofstuff/wiki/WadOfStuffDjangoForms

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top