# Is it safe to modify \`base\_fields\` on admin form classes?

**URL:** <https://forum.djangoproject.com/t/is-it-safe-to-modify-base-fields-on-admin-form-classes/24672>\
**Category:** Forms & APIs\
**Created:** [October 20, 2023, 3:17pm UTC](https://forum.djangoproject.com/t/is-it-safe-to-modify-base-fields-on-admin-form-classes/24672 "2023-10-20T15:17:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![djvg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/djvg/32/5064_2.png) [@djvg](https://forum.djangoproject.com/u/djvg)\
**Post date:** [October 20, 2023, 3:17pm UTC](https://forum.djangoproject.com/t/is-it-safe-to-modify-base-fields-on-admin-form-classes/24672/1 "2023-10-20T15:17:39Z")

</div>

The [forms API documentation](https://docs.djangoproject.com/en/4.2/ref/forms/api/#accessing-the-fields-from-the-form) warns against changing a `Form`’s `base_fields` attribute:

> Beware not to alter the `base_fields` attribute because this modification will influence all subsequent `ContactForm` instances within the same Python process: …

Could someone tell me if this also holds for `ModelFormMetaclass` subclasses that are used in the admin?

For example, the class returned by `ModelAdmin.get_form()` [comes from a `modelform_factory`](https://github.com/django/django/blob/21382e2743d06efbf5623e7c9b6dccf2a325669b/django/contrib/admin/options.py#L835), so I expect `base_fields` can be safely modified here. Is that correct?

Is this explained somewhere in the docs?

Thanks in advance.

---

<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:** [October 20, 2023, 3:44pm UTC](https://forum.djangoproject.com/t/is-it-safe-to-modify-base-fields-on-admin-form-classes/24672/2 "2023-10-20T15:44:49Z")

</div>

I’m curious - I’m not sure I understand why you would want to modify `base_fields` at that point. Once you’ve gotten an instance of the form to use, you can modify the `fields`. What is it that you are looking to accomplish by this that modifying `fields` isn’t going to do for you?  
(And sorry, no, I don’t have an immediate answer for your question.)

---

<div class="post-metadata">

**Author:** ![djvg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/djvg/32/5064_2.png) [@djvg](https://forum.djangoproject.com/u/djvg)\
**Post date:** [October 20, 2023, 4:29pm UTC](https://forum.djangoproject.com/t/is-it-safe-to-modify-base-fields-on-admin-form-classes/24672/3 "2023-10-20T16:29:37Z")

</div>

> … Once you’ve gotten an instance of the form to use, you can modify the `fields`. …

@KenWhitesell Thanks for the quick reply.

A use case would be e.g. extending `ModelAdmin.get_form()` to modify the admin form’s fields dynamically, based on the object being edited.

At this point, we only have access to the `base_fields`, because `ModelAdmin.get_form()` returns a _class_, not an _instance_ (see [source](https://github.com/django/django/blob/21382e2743d06efbf5623e7c9b6dccf2a325669b/django/contrib/admin/options.py#L786)).

However, regardless of the use case, I would like to know if the warning from the forms API docs, concerning `base_fields`, extends to the admin form classes.

I suppose it doesn’t, as the admin form classes are factory generated, but I’m not 100% sure, and I don’t like nasty surprises. 🙂

This is not an xy-problem: I’m aware of several alternative ways to achieve this, e.g. using `self.instance` in the ` __init__ ()` of a custom `ModelForm`, or getting the object based on the `pk` from the `request` in a `formfield_for_*` method, but those are not always convenient.

---

<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:** [October 20, 2023, 7:44pm UTC](https://forum.djangoproject.com/t/is-it-safe-to-modify-base-fields-on-admin-form-classes/24672/4 "2023-10-20T19:44:36Z")

</div>

> [@djvg](#):
>
> However, regardless of the use case, I would like to know if the warning from the forms API docs, concerning `base_fields`, extends to the admin form classes.

While it’s not a definitive answer, I have tried the example in the docs with using two different instances of the same form class created by the `modelform_factory` function, and the problem does not appear. If I create two forms from the same form class from the same output of `modelform_factory`, the problem _does_ occur as documented.

So I agree with your supposition - I cannot see where that warning would apply in this case.

---

<div class="post-metadata">

**Author:** ![djvg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/djvg/32/5064_2.png) [@djvg](https://forum.djangoproject.com/u/djvg)\
**Post date:** [October 20, 2023, 8:19pm UTC](https://forum.djangoproject.com/t/is-it-safe-to-modify-base-fields-on-admin-form-classes/24672/5 "2023-10-20T20:19:40Z")

</div>

@KenWhitesell That’s good to hear. Thanks for the effort! Much appreciated. 🙂
