Fields excluded from model constraint validation

Hi! Long time user, first time poster.

I’m investigating an issue I’m having with constraint validation. The addition of constraint checking to model validation in Django 4.1 is great! We want to rely on it wherever possible, rather than writing duplicative validation code. However, I’m finding that we get IntegrityErrors in some situations where I would expect ValidationErrors to be raised (and smoothly handled by the admin). I’d like to improve this situation, and haven’t found a post/ticket that quite addresses this.

Short story, using Django 4.2 I have a model MyModel, and we edit it in the admin. Its admin interface hides certain fields after object creation (via MyModelAdmin.get_fieldsets). Some of these fields are those that are referenced by model CheckConstraints. The issue appears to be that Model.validate_constraints receives a set of field names to exclude, generated by BaseModelForm._get_validation_exclusions. When validate_constraints runs, CheckConstraint.validate tries to check the constraint, fails to do so due to missing fields, but then suppresses that FieldError. This makes it such that the constraint is never checked during validation, and an IntegrityError is thereafter raised when the object tries to save.

I read some of the issues referenced in _get_validation_exclusions. But I don’t fully understand why any fields should be excluded from validate_constraints. If I was cleaning field data in MyModel.full_clean to satisfy a constraint, then if I called Model.full_clean before doing so, it could peremptorily raise an error before the fields were ready, but I’m not doing so. Overall, it is odd to me that constraint validation silently fails, leading to IntegrityErrors getting raised when the form saves.

Right now, my workaround seems to be to define MyModel.validate_constraints and NOT pass exclude to the super() call therein. Is this relatively safe? Am I missing something above? Should the docs discuss this behavior in more detail?

Thanks in advance for any thoughts! I can add a minimal example if it would be helpful.

I think an example would be very helpful - if nothing else it would provide a baseline if anyone wanted to replicate the situation in their environment.

Sure! Create a default Django project and app and use the following. Here is a models.py:

from django.db import models

class MyModel(models.Model):
    a = models.IntegerField()
    b = models.IntegerField()

    # Uncomment this to get ValidationError on ModelAdmin edit
    # rather than IntegrityError when changing an existing object.
    #
    # def validate_constraints(self, exclude=None):
    #     super().validate_constraints()  # no exclude!

    class Meta:
        constraints = [
            models.CheckConstraint(
                condition=models.Q(a=models.F('b')),
                name='my_check',
            )
        ]

And here is admin.py:

from django.contrib import admin
from .models import MyModel

@admin.register(MyModel)
class QuestionAdmin(admin.ModelAdmin):
    fields = ['a', 'b']

    def get_fields(self, request, obj=None):
        fields = list(self.fields)
        if obj and obj.pk:
            fields.remove('b')
        return fields

This is a bit contrived, but it works (Django 5.1.4 used). Create an object in the admin with a==b, then save it, then edit it to change the one field shown, and you get an IntegrityError. If you uncomment the validate_constraints defined on the model, a nice ValidationError occurs instead. I think this is really a forms + ORM issue, not the admin specifically.

Hello @jaredahern!

The short answer is that while constraint validation was relatively recently introduced it is skipped for constraint that reference excluded fields to be coherent with how unique together and unique for date validation has historically worked.

As for why form initiated model validation excludes all models fields not present in the form I believe that it is to account for the fact the some model fields values (that might be part of constraints) might be assigned after validation occurs and that (long ago) a decision was taken to allow this pattern to be used at the cost of requiring manual constraint validation when its the case.

There also the question of what exactly should be displayed to the user when their form submission results in a constraint violation involving fields values they cannot control (b in your case). In the face of this ambiguity the system was designed to perform validation only when it could be sure it had all the information at hand and forcing users of field shadowing and constraints to implement their own validation logic.

Now in your case this validation logic could take many forms but I believe that the most appropriate one would be to have your QuestionAdmin provide a form override to provide a clean method that calls Question._meta.constraints[0].validate directly.

Thank you @charettes! Apologies, my follow-up here got lengthy…

Got it. It makes sense that it was implemented to be consistent with the uniqueness behavior. However I’m still not clear on the intent behind passing exclude to either Model.validate_unique or Model.validate_constraints.

Excluding fields from Model.clean_fields makes sense to me, as there may need to be instance-wide cleaning in the subsequent Model.clean. Field exclusion from the other two seems to be for a slightly different purpose though. Validation errors there reflect database level errors that will occur on save, so skipping some checks during validation due to excluded fields just masks future issues. There is probably a use case for partial validation I’m not considering, but I wonder whether passing the same exclude set to every method called by Model.full_clean always makes sense.

Agreed that excluding fields from form-level validation and Model.clean_fields makes sense to me for that reason. But I’m not sure it makes sense for the methods called after Model.clean, which I thought was the last place that field values should be set. Are there other places later along the chain in which that can occur?

Absolutely. I think this ties into where the docs or code might not capture this nuance. Model instance reference | Django documentation | Django indicates “ModelForm uses this argument to exclude fields that aren’t present on your form from being validated since any errors raised could not be corrected by the user.” But in the case of a constraint that touches multiple fields, the fact that b can’t be changed doesn’t mean the user can’t correct the problem (understood that somehow showing the b field value would be relevant in my example).

Yes, I see what you are saying. Rechecking the constraints in full makes sense, although I think it means there will be redundant DB queries. I may still be willing to force validate_unique and validate_constraints to ignore the exclude set instead though, since I think we can avoid changing instance fields later than in Model.clean and specially handle any partial validation needs that arise. That might also address similar issues with inlines or in other places without adding more validation code.


A side question is about concurrency: whether validate_unique and validate_constraints (uniqueness and exclusion) and save are invoked atomically by a form within a transaction, as that is another potential source of IntegrityErrors. Poking around at BaseForm.is_valid and other places, I don’t think so. I think this would probably also require something like having full_clean “tentatively” save the instance, and have save just not abort the transaction. But then the reciprocal can occur, where a different save fails validation due to a “tentative” save that gets aborted. I don’t really see a way to avoid these issues without somehow combining database validation and saving into one operation. But none of this is any worse than Python-level constraint validation. Does that all sound correct?

I do agree that it doesn’t mean the user can’t correct the problem but the system was designed around the premise that if it doesn’t have a strong guarantee the user can correct the problem it should refuse the temptation to guess and delegate this responsibility to the developer. Whether or not this is the right design is debatable but the current design is so ossified in the Django ecosystem that it is unlikely to change at this point.

You are correct and it means that due to the way things are currently designed constraint validation is not well suited for a highly concurrent environment. If you host a website with a large amount of traffic that allows users to sign up by themselves and use a standard model form that performs username validation through validate_unique you’ll most likely have noticed monthly logs about IntegrityError resulting from double form submits.

The only way to salvage the current state of things I could think of is to have database backends turn IntegrityError that are tied to constraint validation failures (this is backend dependent) into ConstraintValidationFailure(IntegrityError) instances with a .constraint_name property to maintain PEP 249 compatiblity while allowing upper level abstrations to turn them back into ValidationError.

Assuming this is doable for all backends it would offer a way to have the admin and model class based views to capture ConstraintValidationFailure raised on save, retrieve the proper validation error message from Model._meta.get_constraint(exc.constraint_name).get_violation_error_message(), and raise / handle a proper ValidationError.

Thank you @charettes for all of your insights. They are much appreciated.

That makes sense. Our use case is definitely a bit unusual. I understand the situation much better now though, and can work around it for our use.

Interesting. Yeah, the combination of forms and model behavior does seem to make this challenging. I’ve always wondered about how to make a DB return more explicit errors - this helps clarify that.

My main project now is using DRF APIs, rather than Django forms. So in that case, perhaps there are ways to make the API save process include model constraint validation, without the need for the deeper changes you propose. Time to investigate that more!

For the record, adding such a feature has been discussed here

I arrived at this post via a link from Jacob from Fixed #37433 -- Escaped database error messages logged by Q.check(). by nessita · Pull Request #22176 · django/django · GitHub. One comment from me about having the admin and generic views catch constraint violations on save() and raise ValidationError: I think that belongs in the form layer, not the views. Views don’t know (shouldn’t know) which fields a form renders, so they can’t decide whether an error belongs on a field or is a non-field error (the original example here is exactly a constraint on a field that isn’t on the form). The form/formset layer is the idiomatic validation layer and should own that mapping; the view should only react to “the save produced validation errors” and re-render.

@nessita I do share your position on having the field mapping logic implemented in ModelForm.save instead of at the model admin and model class based views.

My suggestion about having them “handle” this problem was less about whether they should handle it directly or indirectly (e.g. through forms) and more about that they should be adapted to handle the ModelForm.save raise signature changed caused by the introduction of ConstraintValidationFailure.

If we make ModelForm.save() raise ValidationError from ConstraintValidationFailure with the proper metadata (e.g. field mapping) we’ll still need to adapt the admin and generic views to handle these ValidationError adequately.

Are you thinking that we wrap form_valids call to form.save() and return self.invalid_form()? I don’t like that because it introduces a second entry point to invalid_form, but I don’t see a better option.

Are you thinking that we wrap form_valids call to form.save() and return self.invalid_form()?

yes exactly.

I don’t like that because it introduces a second entry point to invalid_form, but I don’t see a better option.

I can’t think of a better option either. What are you concern with invalid_form having two entry point? It’s true that you might have form_valid called first and then form_invalid after (when catching ValidationError on form.save()) but form_invalid would only be called once in this case.

Mainly that it makes it harder to understand the flow of logic with CBVs which is already infamously hard, hence ccbv. I also worry that someone has a weird implementation of a FormView that does something where they can’t call self.invalid_form() by the time form.save() is called. That’s mainly just gut feel though and I don’t think we could ever prove that. Would we consider this a backwards compatible change? Changing the dynamics of how that method gets called seems on the edge of it.

I had been thinking about this from a different angle (thread on the discord). When I thought about it, I arrived at the conclusion that if form.is_valid() isn’t an adequate API for what we need. Instead our form validation errors should be raised as exceptions, leading to an interface like this:

if request.method == "POST":
    try:
        # commented out to show the extended interface of raising exceptions
        # form.is_valid()
        # form.instance.user = request.user
        project = form.save()
        messages.success(request, f"Created project {project.name}.")
        # Return to the same page so another project can be added.
        return redirect(request.path)
    except FormIsInvalid:
        pass
return render(
    request,
    "myapp/project_form.html",
    {"form": form},
)

To support that, we’d end up needing a new version of class based views. Which we could do outside of Django first.

I think this is how I’d do it in the kind of example given. It’s in defining the form that you know you’re excluding a field that needs to be validated, and so it’s there that you need to correct that omission.

In related cases, say providing a parent object, I will usually provide that to the form directly, and then adjust _get_validation_exclusions appropriately to force validation. But I know I’ve provided the value before validation in that case.

The pattern where you set the parent after using commit=False is exactly why you can’t enforce these constraints in all cases — but I prefer not to do that, simply because it splits the validation responsibility between the form and view, which is something to avoid.

For the same reason, I sceptical about adjusting the flows through the view, which (as I look at it) shows that our form class didn’t account for what it needed to in is_valid(). (Ignoring where you need to take the race seriously, and need a different pattern entirely) an error in save() should be a genuine rarity.

This feels like a common enough flow that we should have a documented API to enable this.