invalid literal for int() with base 10
See original GitHub issueI 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.


#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:
- Created 7 years ago
- Comments:17 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I don’t understand, your model field is:
But your form field is:
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.
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 ?