War and Peace

Report a typo

You are working with a model called Book. The Book model has two fields: title (a string) and author (a string). You want to get a book with the title "War and Peace" and the author "Leo Tolstoy" from the database, or create it if it doesn't exist. Which of the following code snippets correctly accomplishes this task?

  1. book, created = Book.objects.get_or_create(title="War and Peace", author="Leo Tolstoy")

  2. book = Book.objects.get(title="War and Peace", author="Leo Tolstoy")

  3. book, created = Book.objects.create(title="War and Peace", author="Leo Tolstoy")

  4. book = Book.objects.get_or_create(title="War and Peace")

Select one option from the list

Create a free account to access the full topic