Opened 4 years ago

Closed 4 years ago

#32515 closed Bug (invalid)

m2m_changed signal not received with custom through model

Reported by: leon hughes Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords: m2m_changed, signal, custom model,
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider the following models:

class Game(models.Model):

    participants = models.ManyToManyField(
        to='card_users.UserProfile',
        related_name='card_games',
        through='cardgame.GameThroughModel',
    )


class GameThroughModel(models.Model):

    game = models.ForeignKey(
        to='card_game.Game',
        on_delete=models.CASCADE,
    )
    participant = models.ForeignKey(
        to='card_users.UserProfile',
        on_delete=models.CASCADE,
    )


class UserProfile(models.Model):
    user = models.OneToOneField(
        to=settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name='profile',
    )

I have a receiver set up to perform further actions when new participants are added using m2m_changed:

@receiver(m2m_changed, sender=Game.participants.through, dispatch_uid='create_initial_round', weak=False)
def create_initial_round(sender, instance, action, **kwargs):
    print(f"{sender}, {instance}, {action}")

The m2m_changed signal is not received. When a standard manytomany model relation is used ie.:

class Game(models.Model):

    participants = models.ManyToManyField(
        to='card_users.UserProfile',
        related_name='card_games',
    )

the signal is received correctly.

Change History (3)

comment:1 by leon hughes, 4 years ago

Keywords: signal custom model added; signals removed

comment:2 by leon hughes, 4 years ago

keyword changes

comment:3 by Mariusz Felisiak, 4 years ago

Resolution: invalid
Status: newclosed

The m2m_changed signal was created for automatically created intermediate models. If you have an explicit through model, you can attached Model signals to it, i.e. pre_save, post_save, pre_delete, and post_delete.

Note: See TracTickets for help on using tickets.
Back to Top