Opened 11 years ago

Closed 9 years ago

Last modified 8 years ago

#9816 closed (invalid)

ModelForm with only FileField or ImageField can not pass validation

Reported by: ogunes Owned by: nobody
Component: Forms Version: 1.0
Severity: Keywords: ModelForm ImageField validation
Cc: rokclimb15, Nicolás Miyasato Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



I want to let my users can change their mugshot without updating their whole profile. When i have a form with only mugshot field, form never pass validaiton. If i add another field to that form it can pass validation now. My model and forms are below.

class Profile(models.Model):
    Profile model
        ('F', _('Female')),
        ('M', _('Male')),

    user                = models.ForeignKey(User, unique=True, verbose_name=_('user'))
    mugshot             = models.ImageField(_('mugshot'), upload_to='uploads/mugshots', blank=True)
    birth_date          = models.DateField(_('birth date'), blank=True, null=True)
    gender              = models.CharField(_('gender'), choices=GENDER_CHOICES, max_length=1, null=True)
    occupation          = models.CharField(_('occupation'), max_length=32, blank=True)
    mobile              = PhoneNumberField(_('mobile'), blank=True)

This form never pass validation:

class MugshotForm(forms.ModelForm):
    mugshot             = forms.ImageField(required=True)
    class Meta:
        model = Profile
        fields = ['mugshot']

This form pass validation:

class MugshotForm(forms.ModelForm):
    gender              = forms.CharField(widget=forms.HiddenInput())
    mugshot             = forms.ImageField(required=True)
    class Meta:
        model = Profile
        fields = ['mugshot', 'gender']

Is this a bug or did i misunderstand something?

Change History (9)

comment:1 Changed 11 years ago by ogunes

Keywords: ModelForm ImageField validation added
Summary: ModelForms with only FileField or ImageField can not pass validationModelForm with only FileField or ImageField can not pass validation

comment:2 Changed 10 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 Changed 10 years ago by rokclimb15

Cc: rokclimb15 added

comment:4 Changed 10 years ago by Nicolás Miyasato

Cc: Nicolás Miyasato added

I tried to reproduce it but was unable to do so...
The code that I used is the one you submitted.

What I don't get is why you are using the forms.BooleanField in the form that you are inheriting from ModelForm...

If you can provide some further info so that I can reproduce it I will try to do so.

thanks a lot!!

comment:5 Changed 10 years ago by Peter Bengtsson

I can not reproduce it either. Here's what I did

class MyModel(models.Model):
    a = models.FileField(upload_to='.')
    b = models.CharField(max_length=123)

class FileOnlyForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['a']

And a view that uses this modelform

def fileupload(request):
    if request.method == "POST":
        form = FileOnlyForm(request.POST, request.FILES)
        print form.is_valid()
        form = FileOnlyForm()
    return render_to_response('fileupload.html', locals(),

With that I was able to conclude that it works. It does NOT matter if there's only one (file)field or more.

comment:6 Changed 10 years ago by Peter Bengtsson

Resolution: invalid
Status: newclosed

Since neither of us can reproduce it. Reopen if you think I've tried to reproduce it wrongly.

comment:7 Changed 9 years ago by Cal Leeming

Resolution: invalid
Status: closedreopened

The reason the form validates in the second example, is because you have two fields specified. In the first one, you only have one.

Django has really messed up weird issues, if u have a list or tuple with only 1 entry.. (some people get this problem, others do not. I don't know why)

To fix this, add a comma "," after the first field:

fields = ('mugshot', )

That works fine for me


Cal Leeming

comment:8 Changed 9 years ago by Karen Tracey

Resolution: invalid
Status: reopenedclosed

Please don't reopen without a specific recreatable case that demonstrates the problem. It is not true that you need more than one field on a form for it to be able to validate. The really messed up weird issue you seem to have tripped across is a standard Python gotcha: ( 'abc' ) is not a single-element tuple in Python. Parentheses don't make a tuple, commas do (see: So you need the trailing comma on a single element when specifying the list of fields if you specify it as a tuple. In the original description lists, not tuples, were used, so that was unlikely the cause of whatever was going on.

comment:9 Changed 8 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top