Python ORM - peewee
Database
Peewee
创建模型和表字段:
from peewee import *
mysql_db = MySQLDatabase('my_database')
class BaseModel(Model):
class Meta:
database = mysql_db
class User(BaseModel):
username = CharField()
# etc, etc
class Meta:
table_name = "database_name"
在数据库中使用 sql 语句创建表字段,然后可以使用 User model 对其进行操作。
Query operators
http://docs.peewee-orm.com/en/latest/peewee/query_operators.html
其中几个需要着重理解注意:
| Comparison | Meaning |
|---|---|
| << | x IN y |
| >> | x IS y |
| % | x LIKE y |
| ** | x ILIKE y |
LIKE 和 ILIKE 操作符可以模糊匹配字符串,LIKE 是一般用法,ILIKE 匹配时则不区分字符串的大小写。
:::warning ORM vs Python Grammar So just remember:
- Use
.in_()and.not_in()instead ofinand notin - Use
&instead of and - Use
|instead of or - Use
~instead of not - Use
.is_null()instead of is None or == None. - Don’t forget to wrap your comparisons in parentheses when using logical operators. :::
修改历史8 次提交
- refactor: reorganize documentation structure and update Navbar componentxiaocheng··
2fb8f42 - chore(project): clean up obsolete configuration and build artifactsxiaocheng··
3574bd3 - new struct for lblogsxiaocheng··
8c9b28e - move blogs to docsxiaocheng··
c8535a0 - update fix: timeline errorchenweigao··
fb4b591 - update Datechenweigao··
3c14689 - init v3chenweigao··
b770fc2 - init v2chenweigao··
505f81b