MCPcopy
hub / github.com/django/django / test_exclude_and_validation

Method test_exclude_and_validation

tests/model_forms/tests.py:559–589  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

557 exclude = "url" # note the missing comma
558
559 def test_exclude_and_validation(self):
560 # This Price instance generated by this form is not valid because the
561 # quantity field is required, but the form is valid because the field
562 # is excluded from the form. This is for backwards compatibility.
563 class PriceFormWithoutQuantity(forms.ModelForm):
564 class Meta:
565 model = Price
566 exclude = ("quantity",)
567
568 form = PriceFormWithoutQuantity({"price": "6.00"})
569 self.assertTrue(form.is_valid())
570 price = form.save(commit=False)
571 msg = "{'quantity': ['This field cannot be null.']}"
572 with self.assertRaisesMessage(ValidationError, msg):
573 price.full_clean()
574
575 # The form should not validate fields that it doesn't contain even if
576 # they are specified using 'fields', not 'exclude'.
577 class PriceFormWithoutQuantity(forms.ModelForm):
578 class Meta:
579 model = Price
580 fields = ("price",)
581
582 form = PriceFormWithoutQuantity({"price": "6.00"})
583 self.assertTrue(form.is_valid())
584
585 # The form should still have an instance of a model that is not
586 # complete and not saved into a DB yet.
587 self.assertEqual(form.instance.price, Decimal("6.00"))
588 self.assertIsNone(form.instance.quantity)
589 self.assertIsNone(form.instance.pk)
590
591 def test_confused_form(self):
592 class ConfusedForm(forms.ModelForm):

Callers

nothing calls this directly

Calls 5

assertRaisesMessageMethod · 0.80
is_validMethod · 0.45
saveMethod · 0.45
full_cleanMethod · 0.45

Tested by

no test coverage detected