我在django admin中使用filter\u horizontal.

enter image description here

有一个葡萄园叫蒙洛特wine 庄.当我try 在搜索框中键入Cháteau时,它会出现.但是,当我在搜索框中键入Chateau(没有外来字符)时,它不会出现.

我想让它在我同时输入Chateau和Chateau时出现.我该怎么做?

推荐答案

此过滤器作为django附带的自定义javascript小部件实现.承包商.管理

https://github.com/django/django/blob/e89f9571352f42c7752b351ba1e651485e5e7c51/django/contrib/admin/static/admin/js/SelectBox.js

我想你可以通过在 Select 框上做个小改动来得到你想要的东西.筛选方法.我们可以使用这个技巧go 除您搜索的所有 node 中的重音和变音符号.

str.normalize('NFD').replace(/[\u0300-\u036f]/g, "");

来源:https://stackoverflow.com/a/37511463/1977847

例如,您可以将一些javascript添加到更改表单的管理模板中,以覆盖原始搜索过滤器.

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates

像这样的东西可能会起作用,扩展内置admin/change_form.html.

{% extends 'admin/change_form.html' %}    
{% block admin_change_form_document_ready %}
{{ block.super }}
<script>
if (window.SelectBox) {
  window.SelectBox.filter = function(id, text) {
    // lowercase and strip accents off search text
    const tokens = text
      .toLowerCase()
      .normalize('NFD')
      .replace(/[\u0300-\u036f]/g, "")
      .split(/\s+/);
    for (const node of SelectBox.cache[id]) {
      node.displayed = 1;
      // lowercase and strip accents off each node text in the filter list
      const node_text = node.text
        .toLowerCase()
        .normalize('NFD')
        .replace(/[\u0300-\u036f]/g, "");

      for (const token of tokens) {
        if (!node_text.includes(token)) {
          node.displayed = 0;
          break;
        }
      }
    }
    SelectBox.redisplay(id);
  }
}
</script>
{% endblock %}

有其他方法可以将自定义javascript添加到管理站点,但这一种方法非常快速且脏.

Python相关问答推荐

七段显示不完整

学习率未更新

如何对行使用分段/部分.diff()或.pct_change()?

过载功能是否包含Support Int而不是Support Int?

Pydantic:如何将对象列表表示为dict(将列表序列化为dict)

按照行主要蛇扫描顺序对点列表进行排序

如何使用Python中的clinicalTrials.gov API获取完整结果?

根据网格和相机参数渲染深度

Python中使用时区感知日期时间对象进行时间算术的Incredit

仅从风格中获取 colored颜色 循环

Python库:可选地支持numpy类型,而不依赖于numpy

根据二元组列表在pandas中创建新列

将pandas Dataframe转换为3D numpy矩阵

未知依赖项pin—1阻止conda安装""

Python逻辑操作作为Pandas中的条件

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

考虑到同一天和前2天的前2个数值,如何估算电力时间序列数据中的缺失值?

如何使用Numpy. stracards重新编写滚动和?

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

使用Python从rotowire中抓取MLB每日阵容