Peewee字段类

模型(Model)类包含一个或多个属性,这些属性是 Peewee 中 Field 类的对象。Base Field 类不是直接实例化的。Peewee 为等效的 SQL 数据类型定义了不同的子类。

Field 类的构造函数具有以下参数

Sr.No Constructor & Description
1 column_name (str)
Specify column name for field.

2

primary_key (bool)
Field is the primary key.

3

constraints (list)
List of constraints to apply to column

4

choices (list)
An iterable of 2-tuples mapping column values to display labels.

5

null (bool)
Field allows NULLs.

6

index (bool)
Create an index on field.

7

unique (bool)
Create an unique index on field.

8

Default
Default value.

9

collation (str)
Collation name for field.

10

help_text (str)
Help-text for field, metadata purposes.

11

verbose_name (str)
Verbose name for field, metadata purposes.

Field类的子类映射到各种数据库中对应的数据类型,比如SQLite、PostgreSQL、MySQL等。

数字字段(Numeric Field)类

Peewee 中的数字字段类如下所示:

Sr.No Field classes & Description
1 IntegerField
Field class for storing integers.

2

BigIntegerField
Field class for storing big integers (maps to integer, bigint, and bigint type in SQLite, PostegreSQL and MySQL respectively).

3

SmallIntegerField
Field class for storing small integers (if supported by database).

4

FloatField
Field class for storing floating-point numbers corresponds to real data types.

5

DoubleField
Field class for storing double-precision floating-point numbers maps to equivalent data types in corresponding SQL databases.

6

DecimalField
Field class for storing decimal numbers. The parameters are mentioned below

  • max_digits (int) Maximum digits to store.

  • decimal_places (int) Maximum precision.

  • auto_round (bool) Automatically round values.

文本字段(Text fields)

Peewee 中可用的文本字段如下:

Sr.No Fields & Description
1 CharField
Field class for storing strings. Max 255 characters. Equivalent SQL data type is varchar.

2

FixedCharField
Field class for storing fixed-length strings.

3

TextField
Field class for storing text. Maps to TEXT data type in SQLite and PostgreSQL, and longtext in MySQL.

二进制字段

Peewee 中的二进制字段描述如下:

Sr.No Fields & Description
1 BlobField
Field class for storing binary data.

2

BitField
Field class for storing options in a 64-bit integer column.

3

BigBitField
Field class for storing arbitrarily-large bitmaps in a Binary Large OBject (BLOB). The field will grow the underlying buffer as necessary.

4

UUIDField
Field class for storing universally unique identifier (UUID) objects. Maps to UUID type in Postgres. SQLite and MySQL do not have a UUID type, it is stored as a VARCHAR.

日期和时间字段

Peewee 中的日期和时间字段如下 –

Sr.No Fields & Description
1 DateTimeField
Field class for storing datetime.datetime objects. Accepts a special parameter string formats, with which the datetime can be encoded.

2

DateField
Field class for storing datetime.date objects. Accepts a special parameter string formats to encode date.

3

TimeField
Field class for storing datetime.time objectsAccepts a special parameter formats to show encoded time.

由于 SQLite 没有 DateTime 数据类型,因此该字段被映射为字符串。

外键字段(ForeignKeyField )

此类用于在两个模型中建立外键关系,从而建立数据库相应的表。此类使用以下参数实例化 :

Sr.No Fields & Description
1 model (Model)
Model to reference. If set to ‘self’, it is a self-referential foreign key.

2

field (Field)
Field to reference on model (default is primary key).

3

backref (str)
Accessor name for back-reference. “+” disables the back-reference accessor.

4

on_delete (str)
ON DELETE action.

5

on_update (str)
ON UPDATE action.

6

lazy_load (bool)
Fetch the related object, when the foreign-key field attribute is accessed. If FALSE, accessing the foreign-key field will return the value stored in the foreign-key column.

外键字段示例

from peewee import *

db = SqliteDatabase('mydatabase.db')
class Customer(Model):
   id=IntegerField(primary_key=True)
   name = TextField()
   address = TextField()
   phone = IntegerField()
   class Meta:
      database=db
      db_table='Customers'

class Invoice(Model):
   id=IntegerField(primary_key=True)
   invno=IntegerField()
   amount=IntegerField()
   custid=ForeignKeyField(Customer, backref='Invoices')
   class Meta:
      database=db
      db_table='Invoices'

db.create_tables([Customer, Invoice])

执行上述脚本时,将运行以下 SQL 语句查询 –

CREATE TABLE Customers (
   id INTEGER NOT NULL
   PRIMARY KEY,
   name TEXT NOT NULL,
   address TEXT NOT NULL,
   phone INTEGER NOT NULL
);
CREATE TABLE Invoices (
   id INTEGER NOT NULL
   PRIMARY KEY,
   invno INTEGER NOT NULL,
   amount INTEGER NOT NULL,
   custid_id INTEGER NOT NULL,
   FOREIGN KEY (
      custid_id
   )
   REFERENCES Customers (id)
);

SQLiteStudio GUI 工具中验证, 表结构如下:

Peewee字段类

其他字段类型

Peewee 中的其他字段类型包括 –

Sr.No Fields & Description
1 IPField
Field class for storing IPv4 addresses efficiently (as integers).

2

BooleanField
Field class for storing boolean values.

3

AutoField
Field class for storing auto-incrementing primary keys.

4

IdentityField
Field class for storing auto-incrementing primary keys using the new Postgres 10 IDENTITYField class for storing auto-incrementing primary keys using the new Postgres 10 IDENTITY column type. column type.

酷客网相关文章:

赞(0)

评论 抢沙发

评论前必须登录!