V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Oct 21, 2019 · 2782 views
    This topic created in 2380 days ago, the information mentioned may be changed or developed.

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

    3 replies    2019-10-22 22:03:57 +08:00
    banxi1988
        1
    banxi1988  
       Oct 22, 2019
    以 Array 为例,搜索的时候可以先把 array 转成 string, 内置方法有 array_to_string 或者 array_to_tsvector.
    然后再用文本搜索那一类东西.
    hushao
        2
    hushao  
    OP
       Oct 22, 2019
    @banxi1988 谢谢,暂时先用的 cast 转 text,Django 里用了原生 sql 语句, 暂时还没找到什么好方法。
    banxi1988
        3
    banxi1988  
       Oct 22, 2019
    @hushao #2 你可以在 Django 添加自定义实现的包装函数,几行代码的事,很简单的.
    这样就不用直接写原生 SQL 了.毕竟原生 SQL 跟 Django ORM 其他操作不方便配合
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5952 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 06:25 · PVG 14:25 · LAX 23:25 · JFK 02:25
    ♥ Do have faith in what you're doing.