Django autocomplete light创建新选择

时间:2018-02-26 19:58:06

标签: django autocomplete

我安装了自动完成灯并根据本教程添加:https://django-autocomplete-light.readthedocs.io/en/master/tutorial.html

models.py:

 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.py:

 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.py:

 #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 '^股票/ $',views.stock,NAME = '存量'),     URL(R '^股票自动填充/ $',views.StocksAutocomplete.as_view(create_field = '名称'),名称= '库存自动完成',),

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 %}

它是在教程中编写的,用于在网址中添加(create_field='name'),以便能够为自动填充列表添加新选项,但我的表单中的自动填充类似于下拉列表,我无法在其中写入任何内容。 它是这样的:

enter image description here

1 个答案:

答案 0 :(得分:0)

问题是因为我在forms.py中使用了这一行,它变得像dropdownlist:

 name=forms.ModelChoiceField(queryset=Stocks.objects.values_list('name',   flat=True).distinct(),label=_('name'))
 class Meta():