Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:38:55+00:00 2026-06-17T20:38:55+00:00

I have the following code: from django import forms from django.core.exceptions import ValidationError class

  • 0

I have the following code:

from django import forms
from django.core.exceptions import ValidationError

class MyAdminForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def clean(self):
        cleaned_data = self.cleaned_data
        max_num_items = cleaned_data['max_num_items']
        inline_items = cleaned_data.get('inlineitem_set', [])

        if len(inline_items) < 2:
            raise ValidationError('There must be at least 2 valid inline items')

        if max_num_items > len(inline_items):
            raise ValidationError('The maximum number of items must match the number of inline items there are')

        return cleaned_data

I thought I could access the formset from the cleaned_data (by using cleaned_data['inlineitem_set']) but that doesn’t seem to be the case.

My questions are:

  1. How do I access the formset?
  2. Do I need to create a custom formset with my custom validation for this to work?
  3. If I need to do that, how do I access the “parent” form of the formset in its clean method?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:38:56+00:00Added an answer on June 17, 2026 at 8:38 pm

    I’ve just solved this for my own project. It does appear, as suggested in your 2nd question, that any inline formset validation requiring access to the parent form needs to be in the clean method of a BaseInlineFormset subclass.

    Happily, the parent form’s instance gets created (or retrieved from the database, if you’re modifying rather than creating it) before the inline formset’s clean is called, and it is available there as self.instance.

    from django.core.exceptions import ValidationError
    class InlineFormset(forms.models.BaseInlineFormSet):
        def clean(self):
            try:
                forms = [f for f in self.forms
                           if  f.cleaned_data
                           # This next line filters out inline objects that did exist
                           # but will be deleted if we let this form validate --
                           # obviously we don't want to count those if our goal is to
                           # enforce a min or max number of related objects.
                           and not f.cleaned_data.get('DELETE', False)]
                if self.instance.parent_foo == 'bar':
                    if len(forms) == 0:
                        raise ValidationError(""" If the parent object's 'foo' is
                        'bar' then it needs at least one related object! """)
            except AttributeError:
                pass
    
    class InlineAdmin(admin.TabularInline):
        model = ParentModel.inlineobjects.through
        formset = InlineFormset
    

    The try-except pattern here is guarding against an AttributeError corner case that I haven’t seen myself but which apparently arises when we try to access the cleaned_data attribute of a form (in self.forms) that failed to validate. Learned about this from https://stackoverflow.com/a/877920/492075

    (NB: my project is still on Django 1.3; haven’t tried this in 1.4)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I am using django-registration app. and have following code in forms.py from django.contrib.auth.forms import
I have the following models code : from django.db import models from categories.models import
I have the following python code: from django.db import models from datetime import datetime
I have the following code: from random import randint,choice add=lambda x:lambda y:x+y sub=lambda x:lambda
I have the following code: from selenium import selenium selenium = selenium(localhost, 4444, *chrome,
I have the following code (inherited from someone): <div class=feedback>&nbsp;</div> <form onsubmit=return false autocomplete=off>
I have the following code in python from selenium import webdriver from selenium.webdriver.common.by import
I have the following code in the urls.py in mysite project. /mysite/urls.py from django.conf.urls.defaults
I have the following Django Model which retrieves 3 records from a database. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.