推荐学习书目
› 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
chaleaochexist
V2EX  ›  Python

django orm 中有没有拆分字段的方法

  •  
  •   chaleaochexist · Jun 21, 2019 · 2299 views
    This topic created in 2669 days ago, the information mentioned may be changed or developed.
    找了半天没找到太合适的.
    伪 sql

    select name.split('/') from name_table;
    5 replies  •  2019-06-21 13:28:11 +08:00
    37Y37
        1
    37Y37  
       Jun 21, 2019
    先查出来再 split,split 放在程序做
    XiaoxiaoPu
        2
    XiaoxiaoPu  
       Jun 21, 2019
    重载 CharField,自定义字段呗
    Vegetable
        3
    Vegetable  
       Jun 21, 2019
    搞不懂你想干什么,你想取出来就是分好的?你可以给 model 定义方法获取吧,何必在数据库层操作?
    chaleaochexist
        4
    chaleaochexist  
    OP
       Jun 21, 2019
    @Vegetable
    @XiaoxiaoPu
    谢谢,只是好奇,最后用的是#1 的方法.
    Vegetable
        5
    Vegetable  
       Jun 21, 2019   ❤️ 1
    class AModel(models.Model):

    field = models.Charfield(...)

    @property
    def splited_field(self):
    if self.field:
    return self.field.split("/")[0]
    else:
    return ""

    没有缩进凑合看一下,这种类似的逻辑我会直接些在 model 层写好,业务层直接调用方法来获取处理好的数据.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2980 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 12:19 · PVG 20:19 · LAX 05:19 · JFK 08:19
    ♥ Do have faith in what you're doing.