0

我創建這個模型中創建新的選擇時:Django的自動填充燈柱4次在自動完成形式

class TitleDescriptionLazyModel(models.Model): 

class Meta: 
    abstract = True 

title = models.CharField(max_length=255) 
description = models.TextField(blank=True) 

def __str__(self): 
    return self.title 

class Jobsite(TitleDescriptionLazyModel): 
    pass 

如文檔(views.py)中描述,其在此自動填充視圖中使用:

class JobsiteAutocomplete(LoginRequiredMixin, autocomplete.Select2QuerySetView): 

def get_queryset(self): 
    if not self.request.user.is_authenticated(): 
     return Jobsite.objects.none() 

    qs = Jobsite.objects.all() 

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

    return qs 

和我的urls.py有設置爲 「create_field」 的標題字段:

urlpatterns = [ 
    JobsiteAutocomplete.as_view(create_field='title'), name='jobsite_autocomplete'), 
] 

但是,當我運行它,我得到傳遞GET請求自動完成視圖,直到我點擊創建按鈕,此時它員額4次在數據庫中創建4個新項目時,我希望它來創建1:

> [22/Aug/2016 20:44:04] "GET /worklogs/jobsite-autocomplete/ HTTP/1.1" 200 356 
[22/Aug/2016 20:44:10] "GET /worklogs/jobsite-autocomplete/?q=som HTTP/1.1" 200 252 
[22/Aug/2016 20:44:11] "GET /worklogs/jobsite-autocomplete/?q=some HTTP/1.1" 200 254 
[22/Aug/2016 20:44:12] "GET /worklogs/jobsite-autocomplete/?q=somewhere HTTP/1.1" 200 116 
[22/Aug/2016 20:44:13] "POST /worklogs/jobsite-autocomplete/ HTTP/1.1" 200 31 
[22/Aug/2016 20:44:13] "POST /worklogs/jobsite-autocomplete/ HTTP/1.1" 200 31 
[22/Aug/2016 20:44:13] "POST /worklogs/jobsite-autocomplete/ HTTP/1.1" 200 31 
[22/Aug/2016 20:44:13] "POST /worklogs/jobsite-autocomplete/ HTTP/1.1" 200 31 

it creates 4 every time... why?

的django = 1.9 蟒= 3.4 Django的自動填充光(3.1.8)

回答

0

我使用jQuery v1.11.3

我升級到jQuery的2.2.4和它按預期工作。