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

  • SEARCH
  • Home
  • 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 9030869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:35:49+00:00 2026-06-16T07:35:49+00:00

I am using Django 1.4 and I want to set validation rules that compare

  • 0

I am using Django 1.4 and I want to set validation rules that compare values of different inlines.

I have three simple classes

In models.py:

class Shopping(models.Model):
    shop_name = models.CharField(max_length=200)

class Item(models.Model):
    item_name = models.CharField(max_length=200)
    cost = models.IntegerField()
    item_shop = models.ForeignKey(Shopping)

class Buyer(models.Model):
    buyer_name = models.CharField(max_length=200)
    amount = models.IntegerField()
    buyer_shop = models.ForeignKey(Shopping)

In admin.py:

class ItemInline(admin.TabularInline):
    model = Item

class BuyerInline(admin.TabularInline):
    model = Buyer

class ShoppingAdmin(admin.ModelAdmin):
    inlines = (ItemInline, BuyerInline)

So for example it is possible to buy a bottle of rhum at 10$ and one of vodka at 8$. Mike pays 15$ and Tom pays 3$.

The goal is to prevent the user from saving an instance with sums that don’t match: what has been paid must be the same as the sum of the item costs (ie 10+8 = 15+3).

I tried:

  • raising ValidationError in the Shopping.clean method. But the inlines aren’t updated yet in clean so the sums are not correct
  • raising ValidationError in the ShoppingAdmin.save_related method. But raising ValidationError here gives a very user unfriendly error page instead of redirecting to the change page with a nice error message.

Is there any solution to this problem? Is client-side (javascript/ajax) validation the most simple?

  • 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-16T07:35:50+00:00Added an answer on June 16, 2026 at 7:35 am

    You could override your Inline formset to achieve what you want. In the clean method of the formset you have access to your Shopping instance through the ‘instance’ member. Therefore you could use the Shopping model to store the calculated total temporarily and make your formsets communicate. In models.py:

    class Shopping(models.Model):
       shop_name = models.CharField(max_length=200)
    
       def __init__(self, *args, **kwargs)
           super(Shopping, self).__init__(*args, **kwargs)
           self.__total__ = None
    

    in admin.py:

    from django.forms.models import BaseInlineFormSet
    class ItemInlineFormSet(BaseInlineFormSet):
       def clean(self):
          super(ItemInlineFormSet, self).clean()
          total = 0
          for form in self.forms:
             if not form.is_valid():
                return #other errors exist, so don't bother
             if form.cleaned_data and not form.cleaned_data.get('DELETE'):
                total += form.cleaned_data['cost']
          self.instance.__total__ = total
    
    
    class BuyerInlineFormSet(BaseInlineFormSet):
       def clean(self):
          super(BuyerInlineFormSet, self).clean()
          total = 0
          for form in self.forms:
             if not form.is_valid():
                return #other errors exist, so don't bother
             if form.cleaned_data and not form.cleaned_data.get('DELETE'):
                total += form.cleaned_data['cost']
    
          #compare only if Item inline forms were clean as well
          if self.instance.__total__ is not None and self.instance.__total__ != total:
             raise ValidationError('Oops!')
    
    class ItemInline(admin.TabularInline):
       model = Item
       formset = ItemInlineFormSet
    
    class BuyerInline(admin.TabularInline):
       model = Buyer
       formset = BuyerInlineFormSet
    

    This is the only clean way you can do it (to the best of my knowledge) and everything is placed where it should be.

    EDIT: Added the *if form.cleaned_data* check since forms contain empty inlines as well.
    Please let me know how this works for you!

    EDIT2: Added the check for forms about to be deleted, as correctly pointed out in the comments. These forms should not participate in the calculations.

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

Sidebar

Related Questions

I want to generate a list using my django model say I have these
I want to set up a Django server that allows certain users to access
I have a problem that I would like to solve using Django's ORM. I
Am using Django's form for my login page. I want to set focus to
i have set up buildout project (django to be specific) that has to run
I have a set of Django models that are used in two databases (i.e.
I am using Django ModelForms. I have a dropdown selector that allows users to
I have a Django-based webapp that is using PostgreSQL as the backend via psycopg2.
I am using Django ModelForms to create a form. I have my form set
I want to build an API service using Django. A basic workflow goes like

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.