2017-04-01 21 views
0

我使用Django可選,我越來越緊張:Django可選不會下降自動完成

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) 

作品,滴下來,但只有attr 「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> 

被正確地加載

如果你能幫助我,請提前致謝。

回答

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放入查詢集的篩選方法中來過濾數據庫中的查詢集。

+0

它工作完美! :)和非常優雅的解決方案..非常感謝你.. – pepew