# build\_widget\_attrs calls self.errors

**URL:** <https://forum.djangoproject.com/t/build-widget-attrs-calls-self-errors/26303>\
**Category:** Django Internals\
**Created:** [December 20, 2023, 2:59pm UTC](https://forum.djangoproject.com/t/build-widget-attrs-calls-self-errors/26303 "2023-12-20T14:59:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve-bradshaw-c](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/steve-bradshaw-c/32/17889_2.png) [@steve-bradshaw-c](https://forum.djangoproject.com/u/steve-bradshaw-c)\
**Post date:** [December 20, 2023, 2:59pm UTC](https://forum.djangoproject.com/t/build-widget-attrs-calls-self-errors/26303/1 "2023-12-20T14:59:35Z")

</div>

I’m playing with the django 5.0 upgrade, and I’m finding that I have some forms failing to render. Looking closer, I notice that build\_widget\_attrs in boundfield.py refers to self.errors.

Line 290:

> ```
> if not widget.is_hidden and self.errors:
> attrs["aria-invalid"] = "true"
> 
> ```

self.errors is a property that can force a form clean(), which may not be what you want, when GETting a form.

Are we to assume that our custom form clean() functions can be called during a GET request as well as after POST?

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [December 20, 2023, 3:19pm UTC](https://forum.djangoproject.com/t/build-widget-attrs-calls-self-errors/26303/2 "2023-12-20T15:19:55Z")

</div>

Hi @steve-bradshaw-c, welcome 👋

`full_clean()` is a no-op for unbound forms:

```auto
        if not self.is_bound: # Stop further processing.
            return

```

(`forms.py:317`)

… so there’s no harm in it being called, I think 🤔

But also, the `.errors` property is not new there, and is called during form rendering in Django 4.2 as well. (in `BaseForm.get_context()`).

**Maybe** there’s some issue though… if you could reduce to a minimal example showing a difference that would be useful (and make it easier to say more)

Thanks.

//cc @smithdc1

---

<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:** [December 20, 2023, 3:43pm UTC](https://forum.djangoproject.com/t/build-widget-attrs-calls-self-errors/26303/3 "2023-12-20T15:43:07Z")

</div>

Also note that it is possible to bind a form with data supplied during a GET request - or even completely outside the context of an HTTP request (think Celery tasks or management commands that pull data from external sources).

So yes, not all forms are bound with data as the result of a POST, and so the clean function should run regardless of the request being made (or not made) to supply data to the form.

---

<div class="post-metadata">

**Author:** ![steve-bradshaw-c](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/steve-bradshaw-c/32/17889_2.png) [@steve-bradshaw-c](https://forum.djangoproject.com/u/steve-bradshaw-c)\
**Post date:** [December 20, 2023, 5:58pm UTC](https://forum.djangoproject.com/t/build-widget-attrs-calls-self-errors/26303/4 "2023-12-20T17:58:33Z")

</div>

I believe the call to .errors is new in build\_widget\_attrs - I checked it against 4.2.8 before posting. In my test case is\_bound is true, triggering my model’s clean() function

It breaks when rendering a ModelForm on a template. The ModelForm is not the Model where the clean() function is, but the Model for the ModelForm is a child of the clean()-broke-Model. If I remove this call, it works. Bizarrely, if I don’t remove it and put a breakpoint on the call to build\_widget\_attrs and inspect the boundfield object (possibly calculating some lazy values) and continue, the page renders fine as well.

It’s difficult to provide an example because I don’t know exactly what’s happening. It seems a clean function on an related model is being run during rendering of a ModelField. I can fix it by shoring up the validation on that clean function, but it still feels a little strange.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [December 20, 2023, 6:08pm UTC](https://forum.djangoproject.com/t/build-widget-attrs-calls-self-errors/26303/5 "2023-12-20T18:08:20Z")

</div>

Hey @steve-bradshaw-c, yes, **as described** it sounds a little strange…

I imagine it’s difficult to reproduce, but if you could provide even a sample project showing a regression here (and [open a ticket](https://code.djangoproject.com/) 😉) we’d be able to investigate.

(Without that it’s hard to say anything much… 😬)
