Bird
0
0

Why does this Django model test NOT raise an error?

medium📝 Debug Q7 of 15
Django - Testing Django Applications
Why does this Django model test NOT raise an error?
class Employee(models.Model):
    salary = models.PositiveIntegerField()

class EmployeeTest(TestCase):
    def test_negative_salary(self):
        emp = Employee(salary=-100)
        emp.save()
ABecause the test case disables validation
BBecause Negative salary is allowed by PositiveIntegerField
CBecause model field validation is not automatically run on save()
DBecause salary field is nullable
Step-by-Step Solution
Solution:
  1. Step 1: Understand PositiveIntegerField behavior

    It expects positive integers but does not enforce validation on save() automatically.
  2. Step 2: Why no error on save()

    Django model field validation runs only when explicitly called or via forms, not on save().
  3. Final Answer:

    Model saves without error because validation is not automatic on save -> Option C
  4. Quick Check:

    save() skips validation unless manually invoked [OK]
Quick Trick: save() does not auto-validate fields [OK]
Common Mistakes:
MISTAKES
  • Assuming save() runs full validation
  • Confusing PositiveIntegerField behavior
  • Expecting errors without calling full_clean()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes