Opened 7 years ago

Closed 6 years ago

Last modified 4 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, nmiyasato Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Hi.

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
    
    """
    
    GENDER_CHOICES = (
        ('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 7 years ago by ogunes

  • Keywords ModelForm ImageField validation added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from ModelForms with only FileField or ImageField can not pass validation to ModelForm with only FileField or ImageField can not pass validation

comment:2 Changed 6 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by rokclimb15

  • Cc rokclimb15 added

comment:4 Changed 6 years ago by nmiyasato

  • Cc nmiyasato 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 6 years ago by peterbe

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()
    else:
        form = FileOnlyForm()
    return render_to_response('fileupload.html', locals(),
                              context_instance=RequestContext(request))

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 6 years ago by peterbe

  • Resolution set to invalid
  • Status changed from new to closed

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

comment:7 Changed 6 years ago by foxwhisper

  • Resolution invalid deleted
  • Status changed from closed to reopened

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

Regards

Cal Leeming

comment:8 Changed 6 years ago by kmtracey

  • Resolution set to invalid
  • Status changed from reopened to closed

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: http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences). 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 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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