invalid literal for int() with base 10: '' using FormSet with File Upload

216 views
Skip to first unread message

Néstor Boscán

unread,
Jan 12, 2016, 3:59:42 PM1/12/16
to Django users
Hi Django Forum

I'm working on a web application that has a page with form set that requires file upload. When I upload the files I get:

Django Version:1.9.1
Exception Type:ValueError
Exception Value:
invalid literal for int() with base 10: ''
Exception Location:C:\desarrollo\Python27\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 976
Python Executable:C:\desarrollo\Python27\python.exe
Python Version:2.7.3

On this line of code:

study_form_set = DiagnosticStudyFormSet(request.POST, request.FILES, prefix='studies', queryset=DiagnosticStudy.objects.filter(diagnostic__id=pk))

My model:

class DiagnosticStudy(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(verbose_name='Name', max_length=255)
    link = models.CharField(verbose_name='Link', max_length=255, blank=True, null=True)
    path = models.FileField(upload_to='files', max_length=255, blank=True, null=True)
    diagnostic = models.ForeignKey(Diagnostic, blank=True, null=True)
    study_type = models.ForeignKey(StudyType, blank=True, null=True)
    class Meta:
        managed = False
        db_table = 't_udm_diagnostic_study'

My forms:

class DiagnosticStudyForm(ModelForm):
    class Meta(object):
        model = DiagnosticStudy
        fields = [ 'id', 'name', 'link', 'path', 'study_type' ]
        widgets = {
   'id': HiddenInput(attrs={ 'class': 'campo' }),
        'name': TextInput(attrs={ 'class': 'campo', 'size': '40', 'onchange': 'validate(this)' }),
'link': TextInput(attrs={ 'class': 'campo', 'size': '40', 'onchange': 'validate(this)' }),
'path': FileInput(attrs={ 'class': 'campo', 'size': '40', 'accept': 'image/gif,image/jpeg,image/pjpeg,image/png,image/tiff,image/x-tiff,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation', 'onchange': 'validate(this)' }),
'study_type': HiddenInput(attrs={ 'class': 'campo', 'size': '40', 'onchange': 'validate(this)' }),
        }

DiagnosticStudyFormSet = modelformset_factory(DiagnosticStudy, form=DiagnosticStudyForm, extra=5, can_delete=True)

The Form does not have a IntegerField only CharFields and ForeignKeys. Any ideas on how to know which field is generating the error?

Regards,

Néstor

Luis Zárate

unread,
Jan 13, 2016, 12:18:36 AM1/13/16
to django...@googlegroups.com
What is the value of pk in

study_form_set = DiagnosticStudyFormSet(request.POST, request.FILES, prefix='studies', queryset=DiagnosticStudy.objects.filter(diagnostic__id=pk)). ?

Is pk an int?  try print type(pk)
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/00b3ccfa-d092-4027-97fe-31fd6a828129%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
"La utopía sirve para caminar" Fernando Birri



Reply all
Reply to author
Forward
0 new messages