From 5f07460a67825bfc3f0129feec94a24bbf6d2a5f Mon Sep 17 00:00:00 2001 From: Ben Cail Date: Tue, 5 Mar 2024 16:36:11 -0500 Subject: [5.0.x] Fixed #35223 -- Made Model.full_clean() ignore fields with db_default when validating empty values. Thanks Brian Ibbotson for the report. Regression in 7414704e88d73dafbcfbb85f9bc54cb6111439d3. Backport of 1570ef02f34037d32218d463342592debccf915c from main. --- tests/field_defaults/tests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'tests') diff --git a/tests/field_defaults/tests.py b/tests/field_defaults/tests.py index c05d966bdb..6a5c75c36a 100644 --- a/tests/field_defaults/tests.py +++ b/tests/field_defaults/tests.py @@ -2,6 +2,7 @@ from datetime import datetime from decimal import Decimal from math import pi +from django.core.exceptions import ValidationError from django.db import connection from django.db.models import Case, F, FloatField, Value, When from django.db.models.expressions import ( @@ -169,6 +170,23 @@ class DefaultTests(TestCase): years = DBDefaultsFunction.objects.values_list("year", flat=True) self.assertCountEqual(years, [2000, datetime.now().year]) + def test_full_clean(self): + obj = DBArticle() + obj.full_clean() + obj.save() + obj.refresh_from_db() + self.assertEqual(obj.headline, "Default headline") + + obj = DBArticle(headline="Other title") + obj.full_clean() + obj.save() + obj.refresh_from_db() + self.assertEqual(obj.headline, "Other title") + + obj = DBArticle(headline="") + with self.assertRaises(ValidationError): + obj.full_clean() + class AllowedDefaultTests(SimpleTestCase): def test_allowed(self): -- cgit v1.3