0
0
Djangoframework~10 mins

Factory Boy for test data in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Factory class from factory_boy.

Django
from factory import [1]
Drag options to blanks, or click blank then click option'
ABaseFactory
BModelFactory
CFactory
DTestFactory
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ModelFactory' which is not the base class.
Trying to import from 'factory_boy' instead of 'factory'.
2fill in blank
medium

Complete the code to define a factory for a Django model named Book.

Django
class BookFactory([1]):
    class Meta:
        model = Book
Drag options to blanks, or click blank then click option'
ADjangoModelFactory
BFactory
CModelFactory
DBaseFactory
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Factory' which is too generic for Django models.
Using 'ModelFactory' which does not exist.
3fill in blank
hard

Fix the error in the factory field definition to generate a sequence of titles.

Django
title = factory.[1](lambda n: f"Book {n}")
Drag options to blanks, or click blank then click option'
ALazyAttribute
BFaker
CSubFactory
DSequence
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'LazyAttribute' which does not provide a sequence number.
Using 'Faker' which generates random data, not sequences.
4fill in blank
hard

Fill both blanks to create a factory field that uses Faker to generate an author name.

Django
author = factory.[1]('name')
Drag options to blanks, or click blank then click option'
ALazyFunction
BFaker
Cfaker
Dlazy_attribute
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lazy_attribute' which is not a submodule of factory.
Using 'LazyFunction' which is unrelated here.
5fill in blank
hard

Fill all three blanks to define a factory with a post-generation hook that adds tags to a book instance.

Django
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)
Drag options to blanks, or click blank then click option'
ADjangoModelFactory
Bpost_generation
Cself
DLazyAttribute
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Factory' instead of 'DjangoModelFactory' for the base class.
Using 'post_generation' as a method name instead of a decorator.
Naming the instance parameter something other than 'self'.