# ModelAdmin readonly fields make CheckConstraint not checked by Model.validate\_contraints and result to IntegrityError

**URL:** <https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682>\
**Category:** The Admin\
**Created:** [July 4, 2025, 1:50pm UTC](https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682 "2025-07-04T13:50:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabien-michel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fabien-michel/32/28324_2.png) [@fabien-michel](https://forum.djangoproject.com/u/fabien-michel)\
**Post date:** [July 4, 2025, 1:50pm UTC](https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682/1 "2025-07-04T13:50:11Z")

</div>

I’ve a field that is used in a CheckContraint :

```auto
constraints = [
    CheckConstraint(
        condition=~(Q(field_a="") & Q(field_b="")),
        name="one_is_required",
        violation_error_message=_("One field is required"),
   )
]

```

The ModelAdmin of the model, set, under conditions field\_b as readonly.

```auto
def get_readonly_fields(self, request, obj=None):
        """
        Return readonly fields
        """
        readonly_fields = super().get_readonly_fields(request, obj)
        if condition:
            readonly_fields = (*readonly_fields, "field_b")
        return readonly_fields

```

When the field is readonly, it is excluded from Model.full\_clean and so from Model.validate\_constraints.  
So the validation doesn’t occur django side, and it crash at DB side with IntegrityError.

This issue is closely related to the one discussed here: [Fields excluded from model constraint validation](https://forum.djangoproject.com/t/fields-excluded-from-model-constraint-validation/37597)

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [July 4, 2025, 3:48pm UTC](https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682/2 "2025-07-04T15:48:23Z")

</div>

This is the same issue discussed [here](https://code.djangoproject.com/ticket/35367).

The `Form` cannot validate any field which is not part of it, thus you need to find a way to add the field to the form and yet to prevent users from editing it, this may be achieved by a custom field which displays the value and adds an _hidden_ input for it.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 4, 2025, 3:55pm UTC](https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682/3 "2025-07-04T15:55:50Z")

</div>

This:

> [@sevdog](#):
>
> this may be achieved by a custom field which displays the value and adds an _hidden_ input for it.

Does not prevent this:

> [@sevdog](#):
>
> hus you need to find a way to add the field to the form and yet to prevent users from editing it,

As users are always able to edit the html of a page and can change the value directly.

The more appropriate solution would be to create a custom form for that ModelAdmin class that performs the necessary test within the form.

---

<div class="post-metadata">

**Author:** ![sevdog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sevdog/32/20692_2.png) [@sevdog](https://forum.djangoproject.com/u/sevdog)\
**Post date:** [July 4, 2025, 8:35pm UTC](https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682/4 "2025-07-04T20:35:30Z")

</div>

> [@KenWhitesell](#):
>
> As users are always able to edit the html of a page and can change the value directly.
> 
> The more appropriate solution would be to create a custom form for that ModelAdmin class that performs the necessary test within the form.

In the custom form this can be done by checking if the pseudo-readonly field is in [form.changed\_data](https://docs.djangoproject.com/en/5.2/ref/forms/api/#django.forms.Form.changed_data), which means that someone is trying to tamper that value. This should raise a validation error.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 4, 2025, 9:44pm UTC](https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682/5 "2025-07-04T21:44:21Z")

</div>

> [@sevdog](#):
>
> In the custom form this can be done by checking if the pseudo-readonly field is in [form.changed\_data](https://docs.djangoproject.com/en/5.2/ref/forms/api/#django.forms.Form.changed_data),

Quite true - but if you’re already creating a custom form, you’re still better off not including that value at all. (Why create even the opportunity for mischief?)

---

<div class="post-metadata">

**Author:** ![fabien-michel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fabien-michel/32/28324_2.png) [@fabien-michel](https://forum.djangoproject.com/u/fabien-michel)\
**Post date:** [July 7, 2025, 7:32am UTC](https://forum.djangoproject.com/t/modeladmin-readonly-fields-make-checkconstraint-not-checked-by-model-validate-contraints-and-result-to-integrityerror/41682/6 "2025-07-07T07:32:58Z")

</div>

Thanks for all your responses.  
I’ve finally opted to override `Model.full_clean` so it enforce that fields from constraints can only be excluded altogether, or not excluded at all.

```auto
def full_clean(self, *args, exclude=None, **kwargs):
    exclude = set(exclude)
    constraint_fields = {"field_a", "field_b"}
    if intersection := constraint_fields.intersection(exclude):
        if len(intersection) < len(constraint_fields):
            exclude -= constraint_fields
    super().full_clean(*args, exclude=exclude, **kwargs)

```

It seems enough to my use case but maybe I miss something
