django-autocomplete-light: invalid literal for int() with base 10
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.
#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 %}
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17 (7 by maintainers)
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 ?