我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用django.db.models.signals.m2m_changed()。
def line_removed(sender, instance, **kwargs): """ Checks study <-> strain associations following line deletion and removes study links from ICE for any strains that are no longer associated with the study. Note that the m2m_changed signal isn't broadcast when lines or studies are deleted. This signal is broadcast is in both cases, so we'll use it to fill the gap. """ if check_ice_cannot_proceed(): return if not (hasattr(instance, 'pre_delete_study') and hasattr(instance, 'pre_delete_strain_ids')): return study_strains = Q(line__study_id=instance.pre_delete_study.pk) with transaction.atomic(savepoint=False): # find the set of strains on the Study after the delete post_delete_strain_ids = set( edd_models.Strain.objects.filter(study_strains).distinct().values_list('id', flat=True) ) # calculate which strains were removed as the set difference removed_strains = instance.pre_delete_strain_ids - post_delete_strain_ids logger.debug('Pre-deletion strains: %s', instance.pre_delete_strain_ids) logger.debug('Post-deletion strains: %s', post_delete_strain_ids) logger.debug('Removed strains: %s', removed_strains) # after transaction commits, schedule Celery task to unlink in ICE. partial = functools.partial(submit_ice_unlink, instance.pre_delete_study, removed_strains) connection.on_commit(partial)
def signals(patching): signals = Mock(name='signals') patching('django.db.models.signals.post_save', signals.post_save) patching('django.db.models.signals.post_delete', signals.post_delete) patching('django.db.models.signals.m2m_changed', signals.m2m_changed) return signals