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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:19:52+00:00 2026-05-27T06:19:52+00:00

I have a model relationship as defined by the examples in the Django docs

  • 0

I have a model relationship as defined by the examples in the Django docs on ManyToMany relationship with an intermediary model.

I know how this normally works, but this is a quick little app that only uses the Django Admin and this is causing a slight bump in the road.

Here is what I have:

class Item(models.Model):
    name = models.CharField(blank=True, max_length=100)
    vat_deductable = models.BooleanField(default=True)
    price = models.DecimalField(max_digits=12, decimal_places=2)

    def __unicode__(self):
        return self.name


class Invoice(models.Model):
    customer = models.ForeignKey(Relation)
    date = models.DateField(default=datetime.datetime.today)
    amount = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    vat_amount = models.DecimalField(max_digits=12, decimal_places=2, blank=True, null=True)
    is_paid = models.BooleanField(default=False)
    paid_on = models.DateField(blank=True, null=True)
    items = models.ManyToManyField(Item, through='SoldItem')

    def __unicode__(self):
        return unicode(self.customer) + u'_' + unicode(self.date)

class SoldItem(models.Model):
    item = models.ForeignKey(Item)
    invoice = models.ForeignKey(Invoice)
    qty = models.IntegerField(default=1, null=True)

Here is what I would like to do:

After all models have been saved (the new instance of the parent model, any new instances of related models and any new instances of intermediary models) I would like to loop over all instances of Item that is associated with the newly saved Invoice so I can add their price and maybe VAT to the two fields Invoice.amount and Invoice.vat_amount

How would I do that? I have tried with custom save methods on both the Invoice model and its ModelAdmin form, but neither place gives the complete picture when new relations are being formed.

Maybe a signal? But which?

EDIT:
I have tried this solution:
https://stackoverflow.com/a/2109177/150033

It would make sense that save_m2m would make sure everything is saved, but it seems that the newest relationship is always missing when trying this.

  • 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-27T06:19:53+00:00Added an answer on May 27, 2026 at 6:19 am

    Ok, after much research I found someone else that solved this http://igorsobreira.com/blog/2011/2/12/change-object-after-saving-all-inlines-in-django-admin/

    In the invoice admin:

    class InvoiceAdmin(admin.ModelAdmin):
        inlines = [
            SoldItemAdmin,
        ]
    
        def response_add(self, request, new_object):
            obj = self.after_saving_model_and_related_inlines(new_object)
            return super(InvoiceAdmin, self).response_add(request, obj)
    
        def response_change(self, request, obj):
            obj = self.after_saving_model_and_related_inlines(obj)
            return super(InvoiceAdmin, self).response_change(request, obj)
    
        def after_saving_model_and_related_inlines(self, obj):
            solditem_changed.send(obj)
            return obj
    

    And our signal:

    solditem_changed = Signal()
    @receiver(solditem_changed)
    def update_invoice(sender, **kwargs):
        if hasattr(sender, 'solditem_set'):
            total = 0
            for item in sender.solditem_set.all():
                total += item.item.price * item.qty
            sender.amount = total
            sender.save()
        else:
            sender.amount = 0
            sender.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this situation I have two models, Comment and Score. The relationship is defined
I have an Account Entity with a @ManyToMany relationship to Role Entity. (This way
I have a model called Answer which has a ForeignKey relationship to another model
I have a Person model that has a foreign key relationship to Book ,
I have a relationship in a Core Data model that feels like it wants
I have a to-many relationship in my data model, and I'd like to get
Say you have a one to one relationship in your entity model. The code
Suppose I have 2 models. The 2nd model has a one-to-one relationship with the
I have a simple has_one/belongs_to relationship between two models. This is a new association
Let's say I have a class structure that is defined below: Class Item(models.Model): ...

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.