推荐学习书目
› 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 · 2364 views
    This topic created in 2694 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
    37Y37
    Jun 21, 2019
    1
    先查出来再 split,split 放在程序做
    XiaoxiaoPu
    XiaoxiaoPu
    Jun 21, 2019
    2
    重载 CharField,自定义字段呗
    Vegetable
    Vegetable
    Jun 21, 2019
    3
    搞不懂你想干什么,你想取出来就是分好的?你可以给 model 定义方法获取吧,何必在数据库层操作?
    chaleaochexist
    chaleaochexist
    OP
    Jun 21, 2019
    4
    @Vegetable
    @XiaoxiaoPu
    谢谢,只是好奇,最后用的是#1 的方法.
    Vegetable
    Vegetable
    Jun 21, 2019❤️1
    5
    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   ·   5438 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 03:16 · PVG 11:16 · LAX 19:16 · JFK 22:16
    ♥ Do have faith in what you're doing.