Django selectable不会降低自动完成功能

时间:2017-04-01 15:09:43

标签: django autocomplete jquery-ui-selectable

我正在使用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”,既不“包含”:

在我的游戏机中:

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>

正确加载

如果你能帮助我,请提前感谢你。

1 个答案:

答案 0 :(得分:0)

试试这个:

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放在查询集的过滤器方法中来过滤数据库中的查询集。