<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def autogroups_states_changed(sender, instance, action, reverse, model, pk_set, *args, **kwargs):
 """ Trigger group membership update when a state is added or removed from an autogroup config. """

    if action.startswith('post_'):
        for pk in pk_set:
            try:
                state = State.objects.get(pk=pk)
                instance.update_group_membership_for_state(state)
            except State.DoesNotExist:
                # Deleted States handled by the profile state change
                pass