CRUD 操作
3 分钟阅读
CRUD Operations - CRUD 操作
CRUD of Object 对象的 CRUD
If the value of the primary key is already known, Read
, Insert
, Update
, Delete
can be used to manipulate the object.
如果主键的值已知, Read
、 Insert
、 Update
、 Delete
可用于操作对象。
|
|
To query the object by conditions see Query in advance
要按条件查询对象,请参阅预先查询
Read 读取
|
|
Read uses primary key by default. But it can use other fields as well:
读取默认情况下使用主键。但它也可以使用其他字段:
|
|
Other fields of the object are set to the default value according to the field type.
对象的其它字段根据字段类型设置为默认值。
For detailed single object query, see One
有关详细的单个对象查询,请参阅一个
ReadOrCreate 读取或创建
Try to read a row from the database, or insert one if it doesn’t exist.
尝试从数据库中读取一行,或在不存在时插入一行。
At least one condition field must be supplied, multiple condition fields are also supported.
必须提供至少一个条件字段,也支持多个条件字段。
|
|
Insert 插入
The first return value is auto inc Id value.
第一个返回值是自动增长的 Id 值。
|
|
After creation, it will assign values for auto fields.
创建后,它将为自动字段分配值。
InsertMulti
Insert multiple objects in one api.
在一次 API 中插入多个对象。
Like sql statement:
类似于 sql 语句:
insert into table (name, age) values("slene", 28),("astaxie", 30),("unknown", 20)
The 1st param is the number of records to insert in one bulk statement. The 2nd param is models slice.
第一个参数是在一个批量语句中要插入的记录数。第二个参数是模型切片。
The return value is the number of successfully inserted rows.
返回值是成功插入的行数。
|
|
When bulk is equal to 1, then models will be inserted one by one.
当批量等于 1 时,模型将被逐个插入。
Update
The first return value is the number of affected rows.
第一个返回值是受影响的行数。
|
|
Update updates all fields by default. You can update specified fields:
默认情况下,Update 会更新所有字段。您可以更新指定字段:
|
|
For detailed object update, see One
有关详细的对象更新,请参阅 One
Delete
The first return value is the number of affected rows.
第一个返回值是受影响的行数。
|
|
Delete will also manipulate reverse relationships. E.g.: Post
has a foreign key to User
. If on_delete is set to cascade
, Post
will be deleted while delete User
.
Delete 还会处理反向关系。例如: Post
具有指向 User
的外键。如果 on_delete 设置为 cascade
,则在删除 User
时将删除 Post
。
After deleting, it will clean up values for auto fields.
删除后,它将清理自动字段的值。
Changed in 1.0.3 After deleting, it will not clean up values for auto fields.
在 1.0.3 中更改删除后,它不会清理自动字段的值。