【问题标题】:Django selectable don´t drops down autocompleteDjango 可选择不要下拉自动完成
【发布时间】:2017-08-26 18:37:05
【问题描述】:

我正在使用 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>

加载正确

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

【问题讨论】:

    标签: django autocomplete jquery-ui-selectable


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 2021-11-05
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      相关资源
      最近更新 更多