- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有自己的城市模型(不是 django-cities-light),在 MySQL 表中有超过 2M 的记录。每次我开始在自动完成字段中键入内容时,htop 表上的 CPU 负载在 mysqld 进程上跳跃超过 200%,因此看起来脚本在每次自动完成时都请求该表。
我想将表放入 memcache 中以避免这种情况,这是我目前所拥有的:
import autocomplete_light
from django.core.cache import cache, InvalidCacheBackendError
from cities.models import City
def prepare_choices(model):
key = "%s_autocomplete" % model.__name__.lower()
try:
qs = cache.get(key)
if cache.get(key): # return if not expired
return qs
except InvalidCacheBackendError:
pass
qs = model.objects.all() # populate cache
cache.set(key, qs, 60*60*24) # if expired or not set
return qs
class CityAutocomplete(autocomplete_light.AutocompleteModelBase):
search_fields = ['city_name']
choices = prepare_choices(City)
autocomplete_light.register(City, CityAutocomplete)
但是还是一直在请求mysql。
有什么建议吗?
我尝试在 django shell 中为 cities 表设置缓存,但该过程因Segmentation fault 消息而中断。
>>> from django.core.cache import cache
>>> qs = City.objects.all()
>>> qs.count()
2246813
>>> key = 'city_autocomplete'
>>> cache.set(key, qs, 60*60*24)
Segmentation fault
但是我能够将较小的表放入缓存中,我希望能够克服这个问题,所以仍然需要答案。
最佳答案
cache.set(key, qs, 60*60*24) Segmentation fault
发生这种情况是因为查询太大了。您需要在过滤后对其进行缓存。
我就是这样做的。不完美,但确实可以很好地处理 500 个元素。
def get_autocomplete(request):
if request.is_ajax():
q = request.GET.get('term', '')
results_list = MY_model.objects.filter(title__contains=q).all()
results = []
for result in results_list:
results.append(result)
data = json.dumps(results)
else:
data = 'Nothing to see here!'
mimetype = 'application/json'
return http.HttpResponse(data, mimetype)
在网上某处找到的。
您最多需要前 10 个元素,因为其余元素会从屏幕中弹出。
关于python - django-autocomplete-light:如何缓存选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23590696/
我正在尝试让 Ace Editor 支持我自己的查询语言的自动完成功能。 查询本身如下所示 city:newyork color:red color:blue 在上述情况下,我希望用户在输入“c”时可
我有一个 Material-UI 组件,我正在使用它,您可以在其中输入某人的姓名,它将提供一个可供选择的人员列表。这是一个非常标准的用例,但是我需要表单中的所选项目与标签不同。 目前,如果您选择标有
我开始使用 atom IDE 来编写 HTML+ CSS 代码。 autocomplete-atom-api 被激活,但仍然没有代码自动完成,也没有当我按下 ctrl+space 时。 这是我的 ke
我成功地使用了 API,但今天早上遇到错误,文本框中出现“糟糕!出了点问题”,用户无法输入。我发现该问题与 key 相关并已修复,但是,这表明可能会出现一些问题,并且由于此阻塞,用户无法完成。我希望能
我无法弄清楚这两个流行插件中的哪一个在 Sublime 3 环境中的自动完成功能中运行得更好。究竟有什么区别? 此外,我想知道他们是否可以在不搞砸的情况下正常工作。你能帮我解决这个问题吗? 可以找到插
我显然遗漏了一些东西,但是当我尝试将自动完成添加到文本框时,我不断收到此消息:.autocomplete 不是函数 _布局(仅头部): @ViewBag.Title - My ASP.NET
是否有一种标准方法可以使用 ElasticSearch 为小字段(例如地名)实现逐字符输入自动完成。 (在撰写本文时,可以通过搜索进行大量讨论,但似乎没有什么是确定的。(另外,我看到有人谈论 Apac
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 3 年前。
我正在使用 rails3-jquery-autocomplete gem 来自动完成用户名,搜索属性是 lastname 但我想显示全名作为显示值。这是我的 Controller 中的内容 autoc
我有一个 jQuery 自动完成插件,当我复制/粘贴到它时,它不会触发。有什么办法让它工作吗? 最佳答案 $("#ID").bind("paste", function () { setTim
我有 2 个自动完成 UITextField这将在输入文本时触发表格 View 。其中一个我正在使用MVPlaceSearchTextField .我使用 this 自定义构建的另一个. 当我输入 t
我在一个页面上有多个自动完成。 当我对所有文本字段使用 jQuery UI AutoComplete 时,它会添加表单的动态选择列表 Entry 1 有一个 CSS 样式,ui-au
我在 Google Places API Web 服务中使用 Place Autocomplete,以下 API 调用工作正常: https://maps.googleapis.com/maps/ap
我已经在 angular 5 项目上安装了primeng。 我正在使用 p-autoComplete 组件,但现在发生此错误。 我应该如何解决这个错误? 这是我的源代码。 Basic 最佳答案 通常
我不断出现这个错误: “无效的 DOM 属性 autocomplete 。您是说 autoComplete 吗?” 当我在 上编译我的 React 项目时谷歌浏览器 . 当我尝试在 上编译代码时火狐
我想创建一个动态自动完成输入,可以使用 metroui 进行更新和它类似 jquery m4q 。基于on its documentation ,我需要在input标签中设置data-autocomp
我有两个 下拉菜单。我想根据第一个下拉列表的选择更新第二个下拉列表的搜索列表。 这是一个准备好的非工作插件:http://plnkr.co/edit/GxQujjAcxYdawlANJd9O?p=pr
我们有一个标准的地址表格(街道 1、街道 2、城市、州、邮政编码、国家/地区)。 当我在 Street 1 添加 Google Places Autocomplete API 的代码时,它会在加载页面
我有 md-autocomplete : {{item.name}} :: {{item.type}} 带指令:ng-enter . 我的目标:当用户按下 En
我正在为一家医院创建一个动态表单。我在这里遇到数据自动完成的问题。问题是在将 json 数组更新为 data-autocomplete=' ' 后,它没有更新 data-autocomplete='
我是一名优秀的程序员,十分优秀!