Django autocomplete light创建新选择
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django autocomplete light创建新选择相关的知识,希望对你有一定的参考价值。
我安装了自动完成灯并根据本教程添加:https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html
models.朋友 :
class Stocks(models.Model):
user=models.ForeignKey(User, null=True)
name=models.CharField(max_length=128,verbose_name=_('stockname'))
number=models.CharField(blank=True,null=True,max_length=64,verbose_name=_('number'))
suffix=models.CharField(blank=True,null=True,max_length=12,verbose_name=_('uffix'))
comment=models.CharField(blank=True,null=True,max_length=264,verbose_name=_('comment'))
def __str__(self):
return str(self.name)+ '-' +str(self.number)
class Meta:
verbose_name=_('Stock')
verbose_name_plural=_('Stocks')
def get_absolute_url(self):
return reverse('BallbearingSite:mystocks' )
forms.朋友 :
class StocksForm(forms.ModelForm):
name=forms.ModelChoiceField(queryset=Stocks.objects.values_list('name', flat=True).distinct(),label=_('name'))
class Meta():
model=Stocks
fields=('name','number','suffix','brand','comment','price')
widgets = {
'name': autocomplete.ModelSelect2(url='stock-autocomplete')
}
views.朋友:
#stock view is for saving in db
def stock(request):
stocks_form=StocksForm(None)
if request.method == "POST":
stocks_form =StocksForm(data=request.POST)
if stocks_form.is_valid():
instance=stocks_form.save()
instance.user=request.user
instance.save()
messages.success(request,"Registered Successfuly" ,extra_tags="savestock")
else:
messages.error(request, "ERROR!")
else:
stocks_form=StocksForm()
return render(request,'BallbearingSite/stock.html',{'stocks_form':stocks_form})
#AUTOCOMPLETE
class StocksAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
# Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return Stocks.objects.none()
qs = Stocks.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
urls.py:url(r'^ stock / $',views.stock,name ='stock'),url(r'^ stock-autocomplete / $',views.StocksAutocomplete.as_view(create_field ='name'),名称=股票自动完成',),
template.html:
<form enctype="multipart/form-data" method="post" >
{% csrf_token %}
{{ stocks_form.as_p }}
{{form.media }}
{% block javascript %}
<input id="savestocks" type="submit" name="" value="submit">
</form>
<script type="text/javascript" src="{% static 'js/jquery-1.12.4.min.js' %}">
</script>
<script type="text/javascript" src="{% static 'js/jquery-ui.min.js' %}">
</script>
<link rel="stylesheet" type="text/css" href="{% static 'css/jquery-ui.css' %}">
{% endblock %}
它是在教程中编写的,可以在url中添加(create_field='name')
,以便能够为自动完成列表添加新选项,但我的表单中的自动完成就像一个下拉列表,我无法在其中写入任何内容。它是这样的:
答案
问题是因为我在forms.py中使用了这一行,它变得像dropdownlist:
name=forms.ModelChoiceField(queryset=Stocks.objects.values_list('name', flat=True).distinct(),label=_('name'))
class Meta():
以上是关于Django autocomplete light创建新选择的主要内容,如果未能解决你的问题,请参考以下文章
django-autocomplete-light:“无法加载结果”
Django:django-autocomplete-light 无法正常工作
使用 django-autocomplete-light 自动完成
django-autocomplete-light 简单用法