From 0e6f5f1430cc878e9b293c55f51c824cc880a8cb Mon Sep 17 00:00:00 2001 From: Radoslav Georgiev Date: Wed, 2 Nov 2022 14:51:01 +0200 Subject: [PATCH] Expand on model validation via constraints - New good stuff coming from Django 4.1 --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bbfc086..5816f10 100644 --- a/README.md +++ b/README.md @@ -306,7 +306,15 @@ class Course(BaseModel): Now, if we try to create new object via `course.save()` or via `Course.objects.create(...)`, we are going to get an `IntegrityError`, rather than a `ValidationError`. -This can actually be a downside to the approach, because now, we have to deal with the `IntegrityError`, which does not always have the best error message. +This can actually be a downside (_this is not the case, starting from Django 4.1. Check the extra section below._) to the approach, because now, we have to deal with the `IntegrityError`, which does not always have the best error message. + +> 👀 ⚠️ 👀 Since Django 4.1, calling `.full_clean` will also check model constraints! +> +> This actually removes the downside, mentioned above, since you'll get a nice `ValidationError`, if your model constraints fail the check (if you go thru `Model.objects.create(...)` the downside still holds) +> +> More on this, here - +> +> For an example test case, check the Styleguide-Example repo - The Django's documentation on constraints is quite lean, so you can check the following articles by Adam Johnson, for examples of how to use them: