Tuesday, January 12, 2016

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

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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.

1 comment:

  1. The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an integer to the int() function . In other words it's either empty, or has a character in it other than a digit.

    You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .

    if val.isdigit():

    The other way to overcome this issue is to wrap your code inside a Python try...except block to handle this error.

    Python2.x and Python3.x

    Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 .

    With Python2.x , int(str(3/2)) gives you "1". With Python3.x , the same gives you ("1.5"): ValueError: invalid literal for int() with base 10: "1.5".

    ReplyDelete