#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 |
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 by , 16 years ago
Keywords: | ModelForm ImageField validation added |
---|---|
Summary: | ModelForms with only FileField or ImageField can not pass validation → ModelForm with only FileField or ImageField can not pass validation |
comment:2 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 16 years ago
Cc: | added |
---|
comment:4 by , 16 years ago
Cc: | added |
---|
comment:5 by , 16 years ago
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 by , 16 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Since neither of us can reproduce it. Reopen if you think I've tried to reproduce it wrongly.
comment:7 by , 15 years ago
Resolution: | invalid |
---|---|
Status: | closed → 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 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → 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.
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!!