From ea6b17b4815d36f6cc7780fdec303d27997eca49 Mon Sep 17 00:00:00 2001 From: Joseano Sousa Date: Thu, 9 Jul 2026 13:52:35 -0300 Subject: [PATCH] Fixed #20023 -- Added custom validation example for admin inlines. Thank you to James Bligh and Sarah Boyce for the review. --- AUTHORS | 1 + docs/ref/contrib/admin/index.txt | 59 +++++++++++++++++--------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/AUTHORS b/AUTHORS index 981cacc69ef6..70f402588f3d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -570,6 +570,7 @@ answer newbie questions, and generally made Django that much better: Jordi J. Tablada Jorge Bastida Jorge Gajon + Joseano Sousa José Tomás Tocino García Josef Rousek Joseph Kocherhans diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index a16da77937d6..0243c2b980f7 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -2236,33 +2236,6 @@ return the uncompressed versions of the various JavaScript files, including .. _jQuery: https://jquery.com -.. _admin-custom-validation: - -Adding custom validation to the admin -------------------------------------- - -You can also add custom validation of data in the admin. The automatic admin -interface reuses :mod:`django.forms`, and the ``ModelAdmin`` class gives you -the ability to define your own form:: - - class ArticleAdmin(admin.ModelAdmin): - form = MyArticleAdminForm - -``MyArticleAdminForm`` can be defined anywhere as long as you import where -needed. Now within your form you can add your own custom validation for -any field:: - - class MyArticleAdminForm(forms.ModelForm): - def clean_name(self): - # do something that validates your data - return self.cleaned_data["name"] - -It is important you use a ``ModelForm`` here otherwise things can break. See -the :doc:`forms ` documentation on :doc:`custom validation -` and, more specifically, the -:ref:`model form validation notes ` for more -information. - .. _admin-inlines: ``InlineModelAdmin`` objects @@ -2368,6 +2341,8 @@ The ``InlineModelAdmin`` class adds or customizes: through to :func:`~django.forms.models.inlineformset_factory` when creating the formset for this inline. + For an example see the section :ref:`admin-custom-validation`. + .. warning:: When writing custom validation for ``InlineModelAdmin`` forms, be cautious of writing validation that relies on features of the parent model. If the @@ -2758,6 +2733,36 @@ any other inline. In your ``admin.py`` for this example app:: See the :doc:`contenttypes documentation ` for more specific information. +.. _admin-custom-validation: + +Adding custom validation to the admin +===================================== + +The admin uses :class:`~django.forms.ModelForm`. To add custom validation, +provide your own ``ModelForm``:: + + class ArticleForm(forms.ModelForm): + def clean_name(self): + # do something that validates your data + return self.cleaned_data["name"] + +Then configure the :class:`ModelAdmin` to use the form:: + + class ArticleAdmin(admin.ModelAdmin): + form = ArticleForm + +Or configure an :class:`InlineModelAdmin` to use the form:: + + class ArticleInline(admin.TabularInline): + model = Article + form = ArticleForm + +It is important that you use a ``ModelForm``; otherwise things can break. See +the :doc:`forms ` documentation on :doc:`custom validation +` and, more specifically, the +:ref:`model form validation notes ` for more +information. + .. _admin-overriding-templates: Overriding admin templates