Git Product home page Git Product logo

Comments (2)

lord63 avatar lord63 commented on July 1, 2024

CharField 和 IntegerField(peewee 数据字段) 其实都是能工作的,看了下感觉应该是 CharField 比较合适吧,因为本身就是直接使用 '20151129' 这样的比较多,而且日期间的比较也是没有问题的。
照理来说好像要用 DateField,这样的话要稍微自定义下数据字段,不然每次都用 date 类型也是麻烦的,所以还是应该 '20151129' 这样的字符串传进去,调用时返回的应该也是 '20151129' 这样的。但这样的话,其实就是存的时候是 date 类型,出去进来都是 string 形式的,感觉就是多此一举了,不如直接使用 CharField。
其实我什么都不知道 >_<

from zhihudaily.

lord63 avatar lord63 commented on July 1, 2024

比如说像这样的自定义字段

class ZhiDateField(Field):
    db_field = 'zhidate'

    def db_value(self, value):
        year, month, day = map(int, [value[:4], value[4:6], value[6:]])
        return date(year, month, day)

    def python_value(self, value):
        return value.replace('-', '')

from zhihudaily.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.