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 6111413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:37:39+00:00 2026-05-23T14:37:39+00:00

Update : Reading directly the django source code i got one undocumented missing piece

  • 0

Update: Reading directly the django source code i got one undocumented missing piece to solve my problem. Thanks to Brandon that solved half of the problem by giving me one of the missing pieces. See my own answer to see my solution (i dont want to mix things here).

I have the following (simplified) models:

Order(models.Model):
    status = models.CharField( max_length=25, choices=STATUS_CHOICES, default='PENDING')
    total = models.DecimalField( max_digits=22, decimal_places=2)

    def clean(self):
        if self.estatus == 'PAID' or self.estatus == 'SENT':
            if len(self.payment.all()) > 0:
                raise ValidationError("The status cannot be SENT or PAID if there is no payment for the order")

Payment(models.Model):
    amount = models.DecimalField( max_digits=22, decimal_places=2 )
    order  = models.ForeignKey(Order, related_name="payment")

    def clean(self):
        if self.amount < self.order.total or self.amount <= 0:
            ValidationError("The payment cannot be less than the order total")

In my admin.py i have:

class paymentInline(admin.StackedInline):
    model   = Payment
    max_num = 1

class OrderAdmin(admin.ModelAdmin):
    model   = Order
    inlines = [ paymentInline, ]

The validation in the clean method of the Order does not work because there is no payment saved when the validation occurs (obviously it has not been saved to the database).

The validation inside the payment works fine (if editing or adding a new payment).

I want to validate if the order has a payment if the status is ‘PAID’ or ‘SENT’, but as i cannot doit the way is in the clean method.

My question is, how can i access the ‘payment.amount’ value entered by the user in the inline (payment) of the Order form, to accomplish my validation? (considering im in the clean method of the Order model)

  • 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-05-23T14:37:39+00:00Added an answer on May 23, 2026 at 2:37 pm

    After reading the django source code i found one property of the BaseInlineFormSet that contains the Parent Instance of the Inline, in my case, the Order instance being edited.

    Brandon gave me another important piece, iterating over the self.forms of the BaseInlineFormSet to get each of the instances (even not saved or not cleaned or empty), in my case, each Payment Instance being edited.

    These are the two pieces of information needed to check if the Order with status ‘PAID’ or ‘SENT’ has a payment or not. Iterate over the cleaned_data of the formset would not give Order data (i.e. when not changing the Order, just changing the Payment, or when not adding a Payment -and an empty Payment- but changing the Order) which is needed to decide to save the model if the order status is different than ‘PAID’ or ‘SENT’, so this method was discarded before.

    The models are keep the same, I only modified the admin.py to add the next:

    class PaymentInlineFormset(forms.models.BaseInlineFormSet):
        def clean(self):
            order = None
            payment = None
    
            if any(self.errors):
                return
    
            # django/forms/models.py Line # 672,674 .. class BaseInlineFormSet(BaseModelFormSet) . Using Django 1.3
            order = self.instance
    
            #There should be only one form in the paymentInline (by design), so  in the iteration below we end with a valid payment data.
            if len(self.forms) > 1:
                raise forms.ValidationError(u'Only one payment per order allowed.')
            for f in self.forms:
                payment = f.save(commit=False)
    
            if payment.amount == None:
                payment.amount = 0
    
            if order != None:
                if order.status in ['PAID', 'SENT'] and payment.amount <= 0:
                    raise forms.ValidationError(u'The order with %s status must have an associated payment.'%order.status)
    
    class pymentInline(admin.StackedInline):
        model   = Payment
        max_num = 1
        formset = PaymentInlineFormset
    
    class OrderAdmin(admin.ModelAdmin):
        inlines = [ paymentInline, ]
    
    admin.site.register(Order, OrderAdmin)
    admin.site.register(Payment)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: After some more reading I see that this problem is totally general, you
Update: Solved, with code I got it working, see my answer below for the
Update: It looks like the problem is when I'm reading the value from the
update SOLVED - the problem is not with my code. there is a design
I was just reading an update from a friend's project, mentioning the use of
Please read my update at the end of question after reading the answers: I'm
I've been reading about UpdateFrom, used to update a business object from the request.
I am trying to allow folks to update records directly if they know the
I have an app on the Store for which we’re readying an update. The
Update: Check out this follow-up question: Gem Update on Windows - is it broken?

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.