Complete the code to import the Factory class from factory_boy.
from factory import [1]
The main class to create factories in Factory Boy is Factory. You import it directly from factory.
Complete the code to define a factory for a Django model named Book.
class BookFactory([1]): class Meta: model = Book
For Django models, Factory Boy provides DjangoModelFactory as the base class to handle Django-specific features.
Fix the error in the factory field definition to generate a sequence of titles.
title = factory.[1](lambda n: f"Book {n}")
The Sequence field generates unique values by passing an increasing number to the lambda function.
Fill both blanks to create a factory field that uses Faker to generate an author name.
author = factory.[1]('name')
The correct syntax is factory.Faker('name') to generate fake author names.
Fill all three blanks to define a factory with a post-generation hook that adds tags to a book instance.
class BookFactory(factory.[1]): class Meta: model = Book @factory.[2] def add_tags(self, create, extracted, [3]): if not create: return if extracted: for tag in extracted: self.tags.add(tag)
The factory class should inherit from DjangoModelFactory. The decorator for post-generation hooks is post_generation. The method parameter for the instance is conventionally named self.