Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to asynchronously get all objects from the model.
Django
async def get_all_books(): books = await Book.objects.[1]().aall() return books
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'all_async()' or 'fetch_all()'.
Forgetting to use 'await' before the ORM call.
✗ Incorrect
In Django async ORM, you use 'all()' with await to get all objects asynchronously.
2fill in blank
mediumComplete the code to asynchronously get a single object by its primary key.
Django
async def get_book(pk): book = await Book.objects.[1](pk=pk) return book
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous 'get()' without 'await'.
Using non-existent methods like 'get_async()'.
✗ Incorrect
Django async ORM uses 'aget()' to asynchronously get a single object by filter.
3fill in blank
hardFix the error in the async save method call.
Django
async def save_book(book): await book.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous 'save()' without 'await'.
Using incorrect method names like 'save_async()'.
✗ Incorrect
Django async ORM uses 'asave()' to asynchronously save model instances.
4fill in blank
hardFill both blanks to asynchronously filter and count objects.
Django
async def count_books_by_author(author_name): count = await Book.objects.[1](author=author_name).[2]() return count
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'afilter()' which does not exist.
Using synchronous 'count()' without 'await'.
✗ Incorrect
Use 'filter()' to filter queryset and 'acount()' to asynchronously count the results.
5fill in blank
hardFill all three blanks to asynchronously create, update, and save a model instance.
Django
async def create_and_update_book(title, new_title): book = await Book.objects.[1](title=title) book.title = [2] await book.[3]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous 'create()' instead of 'acreate()'.
Forgetting to await 'asave()'.
Assigning the wrong variable to book.title.
✗ Incorrect
Use 'acreate()' to asynchronously create, assign new_title to book.title, then 'asave()' to save asynchronously.