Get the field name from a model and a sender from m2m_changed signal.

def _get_m2m_field(model, sender):
    """
    Get the field name from a model and a sender from m2m_changed signal.
    """
    for field in getattr(model, '_tracked_fields', []):
        if isinstance(model._meta.get_field(field), ManyToManyField):
            if getattr(model, field).through == sender:
                return field
    for field in getattr(model, '_tracked_related_fields', {}).keys():
        if isinstance(model._meta.get_field(field), ManyToManyField):
            if getattr(model, field).through == sender:
                return field