title: Django csv file validation in model form clean method
body: <p>Following is the FileModel to upload a csv file in my Django application:</p>
  
  <pre><code>class File(models.Model):
    uploaded_by = models.ForeignKey(
      User,
      on_delete=models.CASCADE,
    )
    csv_file = models.FileField(
      upload_to='csvfiles/',
    )
  </code></pre>
  
  <p>On invocation of the <code>/upload_file</code> url pattern, the <code>upload_csv_file</code> view executes as follows:</p>
  
  <pre><code> def upload_csv_file(request):
     if request.method == 'POST':
        csv_form = CSVForm(request.POST, request.FILES)
        if csv_form.is_valid():
          file_uploaded = csv_form.save(commit=False)
          file_uploaded.uploaded_by = request.user
          csv_form.save()
          return HttpResponse("&lt;h1&gt;Your csv file was uploaded&lt;/h1&gt;")
  
     elif request.method == 'GET':
        csv_form = CSVForm()
        return render(request, './mysite/upload_file.html', {'csv_form': csv_form})
  </code></pre>
  
  <p>In forms.py I am validating the following:</p>
  
  <ul>
  <li>file extension (.csv)</li>
  <li><p>file size (5 mb)</p>
  
  <pre><code>class CSVForm(forms.ModelForm):
  
    class Meta:
     model = File
     fields = ('csv_file',)
  
    def clean_csv_file(self):
     uploaded_csv_file = self.cleaned_data['csv_file']
  
     if uploaded_csv_file:
       filename = uploaded_csv_file.name
       if filename.endswith(settings.FILE_UPLOAD_TYPE):
          if uploaded_csv_file.size &lt; int(settings.MAX_UPLOAD_SIZE):
              # return True
                return uploaded_csv_file
          else:
              raise forms.ValidationError(
                  "Error")
      else:
          raise forms.ValidationError("Error")
  
    return uploaded_csv_file
  
    # no need for a separate def clean()
    # def clean(self):
      # cleaned_data = super(CSVForm, self).clean()
      # uploaded_csv_file = cleaned_data.get('csv_file')
      # return uploaded_csv_file
  </code></pre></li>
  </ul>
  
  <p>However I encounter the following error on submitting the file upload button:</p>
  
  <pre><code>  Attribute error: 'bool' object has no attribute 'get'
  </code></pre>
  
  <p>I am unsure whether the <code>def clean_csv_file(self)</code> is being invoked or not.</p>
  
  <p>There are ways to validate file extension and size within the function-based view, but I would like to validate the file attributes in the ModelForm's <code>clean()</code> method itself.</p>
  
  <p><strong>UPDATE: Solution found</strong></p>
  
  <p>The def clean_csv_file(self) had to return an instance of uploaded_csv_file variable inplace of True.</p>
  
  <p>Also, there is no need for clean() method if clean_field() is present in the ModelForm class.</p>