Create the ``TrackedEvent`` and it's related ``TrackedFieldModification``
    for a m2m modification.
    The first thing needed is to get the m2m field on the object being tracked.
    The current related objects are then taken (``old_value``).
    The new value is calculated in function of ``action`` (``new_value``).
    The ``TrackedFieldModification`` is created with the proper parameters.

    :param model: The model of the object being tracked.
    :param instance: The instance of the object being tracked.
    :param sender: The m2m through relationship instance.
    :param objects: The list of objects being added/removed.
    :param action: The action from the m2m_changed signal.

def _create_tracked_event_m2m(model, instance, sender, objects, action):
    """
    Create the ``TrackedEvent`` and it's related ``TrackedFieldModification``
    for a m2m modification.
    The first thing needed is to get the m2m field on the object being tracked.
    The current related objects are then taken (``old_value``).
    The new value is calculated in function of ``action`` (``new_value``).
    The ``TrackedFieldModification`` is created with the proper parameters.

    :param model: The model of the object being tracked.
    :param instance: The instance of the object being tracked.
    :param sender: The m2m through relationship instance.
    :param objects: The list of objects being added/removed.
    :param action: The action from the m2m_changed signal.
    """
    field = _get_m2m_field(model, sender)
    if field in getattr(model, '_tracked_related_fields', {}).keys():
        # In case of a m2m tracked on a related model
        related_fields = model._tracked_related_fields[field]
        for related_field in related_fields:
            try:
                related_instances = getattr(instance, related_field[1])
            except ObjectDoesNotExist:
                continue
            # FIXME: isinstance(related_instances, RelatedManager ?)
            if hasattr(related_instances, 'all'):
                related_instances = related_instances.all()
            else:
                related_instances = [related_instances]
            for related_instance in related_instances:
                event = _create_event(related_instance, action)
                fieldname = '{0}__{1}'.format(related_field[0], field)
                _create_tracked_field_m2m(
                    event, instance, field, objects, action, fieldname
                )
    if field in getattr(model, '_tracked_fields', []):
        event = _create_event(instance, action)
        _create_tracked_field_m2m(event, instance, field, objects, action)