File: delete.md

package info (click to toggle)
ormar 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,952 kB
  • sloc: python: 24,085; makefile: 34; sh: 14
file content (139 lines) | stat: -rw-r--r-- 3,594 bytes parent folder | download
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Delete data from database

Following methods allow you to delete data from the database.

* `delete(each: bool = False, **kwargs) -> int`


* `Model`
    * `Model.delete()` method


* `QuerysetProxy`
    * `QuerysetProxy.remove()` method
    * `QuerysetProxy.clear()` method

## delete

`delete(each: bool = False, **kwargs) -> int`

QuerySet level delete is used to delete multiple records at once.

You either have to filter the QuerySet first or provide a `each=True` flag to delete
whole table.

If you do not provide this flag or a filter a `QueryDefinitionError` will be raised.

Return number of rows deleted.

```python hl_lines="43-47"
--8<-- "../docs_src/queries/docs005.py"
```

## Model methods

Each model instance have a set of methods to `save`, `update` or `load` itself.

### delete

You can delete model instance by calling `delete()` method on it.

!!!tip
    Read more about `delete()` method in [models methods](../models/methods.md#delete)

## QuerysetProxy methods

When access directly the related `ManyToMany` field as well as `ReverseForeignKey`
returns the list of related models.

But at the same time it exposes subset of QuerySet API, so you can filter, create,
select related etc related models directly from parent model.

### remove

Removal of the related model one by one.

Removes the relation in the database.

If you specify the keep_reversed flag to `False` `ormar` will also delete the related model from the database.

```python
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)
```

```python
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
```

### clear

Removal of all related models in one call.

Removes also the relation in the database.

If you specify the keep_reversed flag to `False` `ormar` will also delete the related model from the database.

```python
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)
```

```python
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)
```

[querysetproxy]: ../relations/queryset-proxy.md