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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:39:01+00:00 2026-06-09T19:39:01+00:00

I need to set custom save and delete methods on a Many-to-Many relation. I

  • 0

I need to set custom save and delete methods on a Many-to-Many relation.

I tried specifying a model with the “through” attribute but this over-complicated my code and introduced some problems. I don’t need any extra field on the Many-to-Many model, just custom save and delete methods.

Is it possible to accomplish this without specifying the “through” attribute?

Here’s code:

class Order(BaseDate):
    #lots of fields
    relateds = models.ManyToManyField('RelatedProduct', verbose_name=_('related products'), blank=True, related_name='order_relateds', through='OrderRelateds')
    # more fields
    total = CurrencyField(verbose_name=_('total'))

    def calculate_total(self):
        cleanses = self.cleanse.taxed_price() * self.quantity
        delivery = DELIVERY_PRICE if self.delivery == 'delivery' else 0
        relateds = 0
        for r in self.relateds.all():
            relateds = relateds + float(r.taxed_price())
        total = float(cleanses) + delivery + relateds
        return total

    def save(self, *args, **kwargs):
        self.total = '%.2f' % self.calculate_total()
        super(Order, self).save(*args, **kwargs)

class OrderRelateds(models.Model):
    order = models.ForeignKey(Order)
    relatedproduct = models.ForeignKey(RelatedProduct, verbose_name=_('related product'))

    class Meta:
        verbose_name = _('Related Product')
        verbose_name_plural = _('Products Related to this Order')

    def __unicode__(self):
        return self.relatedproduct.__unicode__()

    def save(self, *args, **kwargs):
        super(OrderRelateds, self).save(*args, **kwargs)
        self.order.save()

    def delete(self, *args, **kwargs):
        super(OrderRelateds, self).delete(*args, **kwargs)
        self.order.save()

I need to trigger recalculation of total price of the order if any related product (many to many item) is added to or removed from an order.

Edit: this is the code which solved my problem

from django.db.models.signals import m2m_changed
from django.dispatch import receiver

@receiver(m2m_changed, sender=Order.relateds.through)
def recalculate_total(sender, instance, action, **kwargs):
    """
    Automatically recalculate total price of an order when a related product is added or removed
    """
    if action == 'post_add':
        instance.save()
    if action == 'post_remove' or action == 'post_clear':
        instance.save()
  • 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-09T19:39:03+00:00Added an answer on June 9, 2026 at 7:39 pm

    You could make use of django’s m2m_changed, pre_save/post_save, pre_delete/post_delete signals on the Model containing the relation, and do the related logic there.

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

Sidebar

Related Questions

I need to set custom alarm tone in my App. Could anyone please just
How can I set custom font in UINavigationBar ? I need the tahoma font.
I'm developing a custom shipping method for Magento. For this shipping method I need
I need to set title for Custom UIBarButtonItem. I've the following code: UIButton* backToRecent
Need set zoom for camera preview...
I need set a cultureinfo different of others in a currency field. Metadata: [Display(ResourceType
I need Set collection, where its items will be identified by items class. Something
Need to set some attributes of button. For example Checked. I guess it is
I need to set query in my auth script, where in WHERE clause I
I need to set the color of the font every time I add text

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.