从数据库中删除数据
以下方法允许您从数据库中删除数据。
删除
delete(each: bool = False, **kwargs) -> int
QuerySet级别删除用于一次删除多条记录。
您要么必须首先过滤 QuerySet,要么提供each=True 标志来删除整个表。
如果您不提供此标志或过滤器,则会引发 QueryDefinitionError。
返回已删除的行数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 | import asyncio
import databases
import ormar
import sqlalchemy
from examples import create_drop_database
DATABASE_URL = "sqlite:///test.db"
ormar_base_config = ormar.OrmarConfig(
database=databases.Database(DATABASE_URL),
metadata=sqlalchemy.MetaData(),
)
class Book(ormar.Model):
ormar_config = ormar_base_config.copy(tablename="books")
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(
max_length=100,
default="Fiction",
)
@create_drop_database(base_config=ormar_base_config)
async def run_query():
await Book.objects.create(
title="Tom Sawyer", author="Twain, Mark", genre="Adventure"
)
await Book.objects.create(
title="War and Peace in Space", author="Tolstoy, Leo", genre="Fantasy"
)
await Book.objects.create(
title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction"
)
# delete accepts kwargs that will be used in filter
# acting in same way as queryset.filter(**kwargs).delete()
await Book.objects.delete(genre="Fantasy") # delete all fantasy books
all_books = await Book.objects.all()
assert len(all_books) == 2
asyncio.run(run_query())
|
模型方法
每个模型实例都有一组方法来保存、更新或加载自身。
删除
您可以通过调用delete()方法来删除模型实例。
!!!tip 阅读有关模型方法中的delete() 方法的更多信息
QuerysetProxy 方法
当直接访问相关的ManyToMany字段以及ReverseForeignKey时,返回相关模型的列表。
但同时它公开了 QuerySet API 的子集,因此您可以直接从父模型过滤、创建、选择相关模型等。
消除
相关型号一一删除。
删除数据库中的关系。
如果将 keep_reversed 标志指定为 Falseormar 也会从数据库中删除相关模型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 | class Album(ormar.Model):
ormar_config = base_ormar_config.copy()
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
is_best_seller: bool = ormar.Boolean(default=False)
class Track(ormar.Model):
ormar_config = base_ormar_config.copy()
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album)
title: str = ormar.String(max_length=100)
position: int = ormar.Integer()
play_count: int = ormar.Integer(nullable=True)
album = await Album(name="Malibu").save()
track1 = await Track(
album=album, title="The Bird", position=1, play_count=30,
).save()
# remove through proxy from reverse side of relation
await album.tracks.remove(track1, keep_reversed=False)
# the track was also deleted
tracks = await Track.objects.all()
assert len(tracks) == 0
|
清除
一次调用删除所有相关模型。
还删除数据库中的关系。
如果将 keep_reversed 标志指定为 Falseormar 也会从数据库中删除相关模型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 | class Album(ormar.Model):
ormar_config = base_ormar_config.copy()
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
is_best_seller: bool = ormar.Boolean(default=False)
class Track(ormar.Model):
ormar_config = base_ormar_config.copy()
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album)
title: str = ormar.String(max_length=100)
position: int = ormar.Integer()
play_count: int = ormar.Integer(nullable=True)
album = await Album(name="Malibu").save()
track1 = await Track(
album=album,
title="The Bird",
position=1,
play_count=30,
).save()
track2 = await Track(
album=album,
title="Heart don't stand a chance",
position=2,
play_count=20,
).save()
# removes the relation only -> clears foreign keys on tracks
await album.tracks.clear()
# removes also the tracks
await album.tracks.clear(keep_reversed=False)
|