Django 可选择不下拉自动完成

Django selectable don´t drops down autocomplete

我正在使用 django-selectable,我越来越紧张了:|

models.py

@python_2_unicode_compatible    
class Filing(models.Model):


    company = models.CharField(max_length=60, null=True)
    ticker = models.CharField(max_length=5, null=True)
    number = models.CharField(max_length=15, null=True)
    description = models.CharField(max_length=100,  null=True)
    url = models.CharField(max_length=110, null=True)
    created_date = models.DateTimeField(null=True) 


    def __str__(self):
        return self.ticker

lookups.py

from __future__ import unicode_literals
from selectable.base import LookupBase
from selectable.registry import registry
from .models import Filing

class CompanyLookup(LookupBase):

    model = Filing
    search_fields = ('company__icontains', )

registry.register(CompanyLookup)

所以不要工作:(但是

其他lookups.py

from __future__ import unicode_literals
from selectable.base import LookupBase
from selectable.registry import registry
from .models import Filing

class CompanyLookup(LookupBase):

    def get_query(self, request, ticker):
        data= Filing.objects.values_list('company',flat=True)      
        return [x for x in data if x.startswith(ticker)]


registry.register(CompanyLookup)

有效,下降,但仅具有 "startswith" 的属性,我需要 "icontains"。也不行 "istarswith",也不行 "contains":

在我的控制台中:

Request URL:http://127.0.0.1:8000/assets/flash/ZeroClipboard.swf?      noCache=1491057317592
Request Method:GET 
Status Code:404 Not Found
Remote Address:127.0.0.1:8000

和:

 Uncaught ReferenceError: jQuery is not defined
 at jquery.dj.selectable.js?v=0.9.0:390
 (anonymous) @ jquery.dj.selectable.js?v=0.9.0:390




$(document).ready(function () {
    // Patch the django admin JS
    if (typeof(djselectableAdminPatch) === "undefined" || djselectableAdminPatch) {
        djangoAdminPatches();
    }
    // Bind existing widgets on document ready
    if (typeof(djselectableAutoLoad) === "undefined" || djselectableAutoLoad) {
        window.bindSelectables('body');
    }
});
})(jQuery || grp.jQuery);    <------ this is the line 390

我也不明白为什么,因为在查看源代码中

 <script type="text/javascript"src="/static/javascript/jquery.dj.selectable.js"></script>

加载正确

如果您能帮助我,在此先感谢您。

试试这个:

def get_query(self, request, ticker):
    return  list(Filing.objects.filter(company__icontains=ticker).values_list('company', flat=True))

您想过滤数据库中的数据。不在 Python。你的数据库要快得多。当您执行 [x for x in x] 时,您将返回一个列表,而不是查询集。您可以通过将 field__icontains=value 放在查询集的过滤方法中来过滤数据库中的查询集。