Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding custom validation to a field, for the generic view CreateView
    primarykey
    data
    text
    <h3>The problem</h3> <p>Add custom validation to a form field in Django 1.3, the form is created by the generic view CreateView.</p> <h3>The model</h3> <pre><code>class Picture(models.Model): file = models.ImageField(upload_to=get_image_path) filename = models.CharField(max_length=50, blank=True) user = models.ForeignKey(User, editable=False) upload_date = models.DateTimeField(auto_now_add=True,editable=False) </code></pre> <h3>Generic view CreateView, a bit modified</h3> <pre><code>class PictureCreateView(CreateView): model = Picture def clean_file(self,form): if image: if image._size &gt; settings.MAX_IMAGE_SIZE: raise ValidationError("Image file too large ( &gt; 20mb )") else: raise ValidationError("Couldn't read uploaded image") def get_form(self, form_class): form = super(PictureCreateView, self).get_form(form_class) form.instance.user = self.request.user return form def form_invalid(self, form): ...omitted none important code... response = JSONResponse(data, {}, response_mimetype(self.request)) response['Content-Disposition'] = 'inline; filename=files.json' return response # Called when we're sure all fields in the form are valid def form_valid(self, form): ...omitted none important code... response = JSONResponse(data, {}, response_mimetype(self.request)) response['Content-Disposition'] = 'inline; filename=files.json' return response </code></pre> <h2>My question is: How can I do custom validation on the file field, before form_valid() is reached?</h2> <h3>In short, what I've done so far</h3> <p>According to the documentation here - <a href="https://docs.djangoproject.com/en/dev/ref/forms/validation/" rel="nofollow">https://docs.djangoproject.com/en/dev/ref/forms/validation/</a> I should be able to override the file fields form validator, which I'm trying to do with clean_file() "The clean_() method in a form subclass - where is replaced with the name of the form field attribute. " This would have been easy if I had created the form manually, but it's created by Django from the model by the generic view. </p> <p>My current solution, which is a ugly hack: You can see that I've overridden form_valid() and form_invalid(), in form_valid() I now call clean_file() and if it's a error I call form_invalid(). This creates a few problems, for example I need to create the error message response manually.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload