V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
hushao
V2EX  ›  Python

Django 的 ArrayField 和 JsonField 不支持模糊查询吗?

  •  
  •   hushao · 2019-10-21 22:39:01 +08:00 · 2281 次点击
    这是一个创建于 1620 天前的主题,其中的信息可能已经有所发展或是发生改变。

    某个字段非常适合使用 ArrayField 来实现,但是其中需要针对该字段做模糊搜索,,很惭愧我不会了,,特来请教。🤣

    3 条回复    2019-10-22 22:03:57 +08:00
    banxi1988
        1
    banxi1988  
       2019-10-22 16:34:54 +08:00
    以 Array 为例,搜索的时候可以先把 array 转成 string, 内置方法有 array_to_string 或者 array_to_tsvector.
    然后再用文本搜索那一类东西.
    hushao
        2
    hushao  
    OP
       2019-10-22 19:32:58 +08:00
    @banxi1988 谢谢,暂时先用的 cast 转 text,Django 里用了原生 sql 语句, 暂时还没找到什么好方法。
    banxi1988
        3
    banxi1988  
       2019-10-22 22:03:57 +08:00
    @hushao #2 你可以在 Django 添加自定义实现的包装函数,几行代码的事,很简单的.
    这样就不用直接写原生 SQL 了.毕竟原生 SQL 跟 Django ORM 其他操作不方便配合
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1231 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:04 · PVG 02:04 · LAX 11:04 · JFK 14:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.