@@ -195,6 +195,48 @@ await User.delete_all(users)
195195await User.destroy(1 , 2 , 3 ) # Deletes users with IDs 1, 2, and 3
196196```
197197
198+ #### Temporary Records
199+
200+ If you need to create a record for a short period of time, you can use the
201+ ` with ` statement:
202+
203+ ``` python
204+ with User(name = ' Bob' , age = 30 ) as user:
205+ ...
206+ ```
207+
208+ The ` with ` statement will create the record using the [ ` save() ` ] ( #save ) method.
209+ The record will be deleted at the end of the block using the [ ` delete() ` ] ( #delete ) .
210+
211+ Here is an example of using temporary records to test the [ ` isnull ` ] ( smart-query-mixin.md#isnull ) filter operator:
212+
213+ ``` python
214+ post1 = Post(
215+ title = ' Lorem ipsum' ,
216+ body = ' Lorem ipsum dolor sit amet, consectetur adipiscing elit.' ,
217+ rating = 4 ,
218+ user_id = 1 ,
219+ topic = ' Some topic' , # this post has a topic
220+ )
221+ post2 = Post(
222+ title = ' Lorem ipsum' ,
223+ body = ' Lorem ipsum dolor sit amet, consectetur adipiscing elit.' ,
224+ rating = 4 ,
225+ user_id = 1 , # this post does not have a topic
226+ )
227+
228+ # post1 and post2 will be deleted at the end of the block
229+ async with post1, post2:
230+
231+ # If isnull=True, only posts without a topic must be returned
232+ posts = await Post.where(topic__isnull = True ).all()
233+ assert all ([p.topic is None for p in posts]) is True
234+
235+ # If isnull=False, only posts with a topic must be returned
236+ posts = await Post.where(topic__isnull = False ).all()
237+ assert all ([post.topic is not None for p in posts]) is True
238+ ```
239+
198240### Querying
199241
200242#### Basic Queries
0 commit comments