Bird
0
0

Given this factory:

medium📝 Debug Q6 of 15
Django - Testing Django Applications
Given this factory:
class ProductFactory(factory.DjangoModelFactory):
    class Meta:
        model = Product
    name = factory.Sequence(lambda n: f"Product {n}")
    price = factory.Sequence(lambda n: 10.0 + n)

Why might running tests raise an AttributeError related to 'lower'?
ABecause <code>factory.Sequence</code> cannot be used with numeric values
BBecause <code>name</code> must be a static string, not a Sequence
CBecause the model <code>Product</code> is missing a <code>lower</code> method
DBecause <code>price</code> should be a simple value, not a Sequence
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error cause

    The error 'Sequence object has no attribute lower' usually occurs when a field expects a string but receives a Sequence object.
  2. Step 2: Analyze the factory fields

    The name field uses Sequence correctly to generate strings. The price field incorrectly uses Sequence to generate floats, which is not expected.
  3. Step 3: Correct usage

    Use a fixed float value or a factory.LazyAttribute for numeric fields instead of Sequence.
  4. Final Answer:

    Because price should be a simple value, not a Sequence -> Option D
  5. Quick Check:

    Numeric fields should not use Sequence directly [OK]
Quick Trick: Use Sequence only for string fields, not numeric [OK]
Common Mistakes:
MISTAKES
  • Using Sequence for numeric fields
  • Assuming Sequence works for all field types
  • Ignoring type expectations of model fields

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes