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
XIVN1987
V2EX  ›  Python

peewee 菜鸟入门

  •  1
     
  •   XIVN1987 · 2018-11-22 13:31:53 +08:00 · 2329 次点击
    这是一个创建于 1954 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import os
    import datetime
    import peewee as pw
    from werkzeug.security import generate_password_hash, check_password_hash
    
    
    database = pw.SqliteDatabase('db.sqlite')
    
    
    class Model(pw.Model):
        class Meta:
            database = database
    
    
    class User(Model):
        username = pw.CharField()
        password_hash = pw.CharField()
    
        @property
        def password(self):
            raise AttributeError('password is not readable')
    
        @password.setter
        def password(self, password):
            self.password_hash = generate_password_hash(password)
    
        def verify_password(self, password):
            return check_password_hash(self.password_hash, password)
        
        def __str__(self):
            return '<User %r>' %self.username
    
    
    class Category(Model):
        tag = pw.CharField()
        count = pw.IntegerField(default=0)
    
        def __str__(self):
            return '<Category %r>' %self.tag
    
    
    class Post(Model):
        title = pw.CharField()
        body  = pw.TextField()
        summary = pw.CharField()
    
        category = pw.ForeignKeyField(Category, backref='posts')
        
        timestamp = pw.DateTimeField(default=datetime.datetime.now)
    
        def __str__(self):
            return '<Post %r>' %self.title
    
    
    
    if __name__ == "__main__":
        if not os.path.exists('db.sqlite'):
            User.create_table()
            Post.create_table()
            Category.create_table()
    
            categoryC = Category.create(tag='C')
            categoryPy = Category.create(tag='Py')
    
            Post.create(category=categoryC, title='learn c', body='learn c hardly', summary='').save()
            Post.create(category=categoryPy, title='learn py', body='learn py hardly', summary='').save()
            Post.create(category=categoryPy, title='learn flask', body='learn flask hardly', summary='').save()
    
            for category in Category.select():
                print(category)
    
                for post in category.posts:
                    print('  ', post)
    
    5 条回复    2018-11-22 21:29:36 +08:00
    XIVN1987
        1
    XIVN1987  
    OP
       2018-11-22 13:36:25 +08:00
    网上的代码是用__repr__,,我试了下不行,,可能新版本改用__str__了
    rogwan
        2
    rogwan  
       2018-11-22 13:44:24 +08:00 via iPhone
    楼主对比过 sqlalchemy 没,用起来有什么差别吗?
    XIVN1987
        3
    XIVN1987  
    OP
       2018-11-22 13:53:29 +08:00
    @rogwan
    简单看过网上两个例子,,感觉 sqlalchemy 写起来更麻烦些,,比如定义外键时需要两边都得定义

    我就做个小博客,,用不着 sqlalchemy 那么高级的东西
    rogwan
        4
    rogwan  
       2018-11-22 13:56:21 +08:00 via iPhone
    @XIVN1987 反正从来不用外键,觉得 sqlalchemy 有些函数功能还是蛮强大的,没用过 pw,没法对比。
    keven2000
        5
    keven2000  
       2018-11-22 21:29:36 +08:00
    sqlalchemy 基本对付大部分场景了,可 raw sql、sql expression、orm
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2902 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:57 · PVG 21:57 · LAX 06:57 · JFK 09:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.