question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

invalid literal for int() with base 10

See original GitHub issue

I am using autocomplete-light to suggest names in a form field. It is go well in the createView ,but when I want to change a value in the very same field it tlod me “invalid literal for int() with base 10:” error. I’v been read all the related issues and did not have a clue.Can some one help me. 2016-09-18 12 14 44

2016-09-18 12 08 29

#forms.py
class CaoZuoPiaoForm_zhengling(forms.ModelForm):
    yunweizhan = forms.ModelChoiceField(YunWeiZhan.objects.all())
    nipiaoren=forms.ModelChoiceField(queryset=UserProfile.objects.all(),widget=autocomplete.ModelSelect2(url='bdgqpt:name-autocomplete'))
    shenpiaoren=forms.ModelChoiceField(queryset=UserProfile.objects.all(),widget=autocomplete.ModelSelect2(url='bdgqpt:name-autocomplete'))
    caozuoren=forms.ModelChoiceField(queryset=UserProfile.objects.all(),widget=autocomplete.ModelSelect2(url='bdgqpt:name-autocomplete'))
    jianhuren=forms.ModelChoiceField(queryset=UserProfile.objects.all(),widget=autocomplete.ModelSelect2(url='bdgqpt:name-autocomplete'))
    zhibanfuzeren=forms.ModelChoiceField(queryset=UserProfile.objects.all(),widget=autocomplete.ModelSelect2(url='bdgqpt:name-autocomplete'))
    falinshijian = forms.DateTimeField(widget=DateTimeWidget(usel10n=True,bootstrap_version=3,
        options=dateTimeOptions,attrs=dateTimeAtts),required=False)
    kaishishijian=forms.DateTimeField(widget=DateTimeWidget(usel10n=True,bootstrap_version=3,
        options=dateTimeOptions,attrs=dateTimeAtts),required=False)
    jiesushijian=forms.DateTimeField(widget=DateTimeWidget(usel10n=True,bootstrap_version=3,
        options=dateTimeOptions,attrs=dateTimeAtts),required=False)

    class Meta:
        model = CaoZuoPiao
        fields = "__all__"
    def __init__(self, *args, **kwargs):
        super(CaoZuoPiaoForm_zhengling, self).__init__(*args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-md-4'
        self.helper.field_class = 'col-md-8'
        self.helper.layout=Layout(
            'yunweizhan',
            'biandiansuo',
            'leibie',
            'bianhao',
            'caozuoneirong',
            'nipiaoren',
            'shenpiaoren',
            'falinren',
            'falinshijian', 
            'kaishishijian',
            'jiesushijian',
            'caozuoren',
            'jianhuren',
            'zhibanfuzeren',  
            'bushu',
            'zhuangtai',                   
            ButtonHolder(Submit('submit','Submit')),
            )

#views.py
def caozuopiao_update(request, id=None):
    if not request.user.is_authenticated():
        return render(request, 'bdgqpt/login.html')
    else:       
        instance = get_object_or_404(CaoZuoPiao, id=id)
        form = CaoZuoPiaoForm_zhengling(request.POST or None,instance=instance)
        title="Modify"
        if form.is_valid():
            instance.save()
            messages.success(request,"Success")
            return redirect('bdgqpt:caozuopiaohomepage')
        context = {
            "form": form,
            "title":title,
        }
        return render(request, 'bdgqpt/caozuopiao/caozuopiao_update_form.html', context)

caozuopiao_update_form.html
{% extends 'bdgqpt/base.html' %}
{% load crispy_forms_tags %}
{% block title %}{{title}}|{{block.super}}{% endblock %}
{% block liangpiao_active %}active{% endblock %}
{% block body %}
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12 col-md-6 col-sm-offset-3">
            <div class="panel panel-default">
                <div class="panel-body">
                    <h3>{{title}}</h3>
                     {% if error_message %}
                        <p><strong>{{ error_message }}</strong></p>
                    {% endif %}
                    {{form.as_p}}
                </div>
            </div>
        </div>

{% endblock body %}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
jpiccommented, Sep 26, 2016

I don’t understand, your model field is:

nipiaoren = models.CharField(max_length=10)

But your form field is:

nipiaoren=forms.ModelChoiceField(queryset=UserProfile.objects.all(),widget=autocomplete.ModelSelect2(url='bdgqpt:name-autocomplete'))

You should use ModelChoiceField/ModelSelect2 for ForeignKey, not for CharField, unless you really know what you’re doing.

CharField may contain any string, ModelChoiceField expects an integer value. So that’s why your CharField may crash your ModelChoiceField by providing a value that doesn’t cast to integer.

1reaction
jpiccommented, Sep 22, 2016

Would you happen to be using a ModelChoiceField with a ModelSelect2Multiple instead of a ModelSelect2 or a ModelMultipleChoiceField with a ModelSelect2 instead of a ModelSelect2Multiple ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError: invalid literal for int() with base 10
The reason is that you are getting an empty string or a string as an argument into int. Check if it ...
Read more >
ValueError: invalid literal for int() with base 10
The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that's not an...
Read more >
Python ValueError: invalid literal for int() with base 10
This error can frequently occur when converting user-input to an integer-type using the int() function. This problem happens because Python stores the input...
Read more >
ValueError: invalid literal for int() with base 10 in Python
The Python "ValueError: invalid literal for int() with base 10" occurs when we pass a string that cannot be directly converted to an...
Read more >
Python ValueError: invalid literal for int() with base 10
Our error message tells us there is an invalid literal for an integer in base 10. This means the value we have passed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found