2016-11-26 129 views
2

我是django上的新成員,我需要你的幫助,嘗試多天後才能理解django-autocomplete-light,設置我的測試後, http://192.168.0.108:8000/country-autocomplete/工作中,數據顯示,喜歡這裏explaned http://django-autocomplete-light.readthedocs.io/en/master/tutorial.html#overviewdjango-autocomplete-light error ='list'object has no attribute'queryset'

但是,隨着下一步後,我得到的錯誤:

AttributeError at /auto 
'list' object has no attribute 'queryset' 
Request Method: GET 
Request URL: http://192.168.0.108:8000/auto 
Django Version: 1.10.3 
Exception Type: AttributeError 
Exception Value:'list' object has no attribute 'queryset' 
Exception Location: /home/alcall/ENV/lib/python3.4/site-packages/dal/widgets.py in filter_choices_to_render, line 161 

下面我的設置:

網址:

from dal import autocomplete 
from django.conf.urls import url 
from django.contrib import admin 
from rates.view.index import * 
from rates.view.index import UpdateView 

urlpatterns = [ 
url(r'^admin/', admin.site.urls), 
url(
    r'^country-autocomplete/$', 
    CountryAutocomplete.as_view(), 
    name='country-autocomplete', 
), 
url(r'^auto$', 
    UpdateView.as_view(), 
    name='select', 
), 
] 

models.py

from __future__ import unicode_literals 
from django.db import models 

class Country(models.Model): 
    enabled = models.IntegerField() 
    code3l = models.CharField(unique=True, max_length=3) 
    code2l = models.CharField(unique=True, max_length=2) 
    name = models.CharField(unique=True, max_length=64) 
    name_official = models.CharField(max_length=128, blank=True, null=True) 
    prix = models.FloatField() 
    flag_32 = models.CharField(max_length=255, blank=True, null=True) 
    flag_128 = models.CharField(max_length=255, blank=True, null=True) 
    latitude = models.DecimalField(max_digits=10, decimal_places=8,  blank=True,$ 
    longitude = models.DecimalField(max_digits=11, decimal_places=8, blank=True$ 
    zoom = models.IntegerField(blank=True, null=True) 

    class Meta: 
     managed = False 
     db_table = 'country' 

    def __str__(self): 
     return self.name 

觀點(包括形式太)

from dal import autocomplete 
from django.shortcuts import render 
from rates.models import Country 
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 
from django.http import HttpResponse 
from django import forms 
from django.core.urlresolvers import reverse_lazy 
from django.views import generic 

class CountryAutocomplete(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 Country.objects.none() 

     qs = Country.objects.all() 

     if self.q: 
      qs = qs.filter(name__istartswith=self.q) 

     return qs 

class Form_country(forms.ModelForm): 
    class Meta: 
     model = Country 
     fields = ('name', 'code2l') 
     widgets = { 
      'name': autocomplete.ModelSelect2Multiple(
      'country-autocomplete' 
      ) 
     } 

class UpdateView(generic.UpdateView): 
    model = Country 
    form_class = Form_country 
    template_name = 'fr/public/monformulaire.html' 
    success_url = reverse_lazy('select') 


    def get_object(self): 
     return Country.objects.first() 
+1

請驗證您分享的代碼。它看起來不正確。 - 例如,'url'模式的目標是'autos',但你的'url'只是'auto' – karthikr

+0

我把它從r'^ autos?$改爲r'^ auto $',仍然是issu – Kef

+0

請完整追溯 – jpic

回答

2

我有同樣的問題。這裏的問題與小部件有關。試圖修復它很長時間。爲我工作的唯一方法是更改​​窗體的小部件。

如果沒關係,你可以用autocomplete.ListSelect2代替它,它對我很有用。

那麼試試這個:

class Form_country(forms.ModelForm): 
    class Meta: 
     model = Country 
     fields = ('name', 'code2l') 
     widgets = { 
      'name': autocomplete.ListSelect2(
      'country-autocomplete' 
      ) 
     } 

其實你可以嘗試其他任何自動完成構件和看天氣它的工作原理

0

我你在__init__()創建窗口小部件,那麼這個變通的issue #790幫助:

form.fields['name'].widget.choices = form.fields['name'].choices 
相關問題