Complete the code to get a single Book object by its id.
book = Book.objects.[1](id=5)
The get() method returns a single object matching the query. Here, it fetches the Book with id=5.
Complete the code to get a User object by username.
user = User.objects.[1](username='alice')
get() fetches a single User object with the username 'alice'.
Fix the error in the code to get a Product by its sku.
product = Product.objects.[1](sku='12345')
Using get() is correct to fetch a single Product by sku. Using filter() returns a queryset, which may cause errors if you expect a single object.
Fill both blanks to get a Customer by email and handle the case if not found.
try: customer = Customer.objects.[1](email=[2]) except Customer.DoesNotExist: customer = None
get() fetches the single Customer with the given email. The email string must be quoted. The exception handles the case when no customer is found.
Fill all three blanks to get an Order by order_id, check if it exists, and assign None if not.
try: order = Order.objects.[1]([2]=[3]) except Order.DoesNotExist: order = None
get() fetches the single Order with order_id=42. The key and value must be correct. The exception handles the case when no order is found.