import logging


def clean_model_fields(model_fields):
    """
    Given a list of models and search_field dictionaries, returns the provided
    list with any fields removed that were not found on their respective models

    Expected input is a list of two tuples in the form of:
        [(ModelClass, search_fields_dictionary), ...]
    """
    for ModelKls, fields in model_fields:
        model_field_names = [field.name for field in ModelKls._meta.fields]
        field_keys = fields.keys()
        for field_key in field_keys:
            if field_key not in model_field_names:
                fields.pop(field_key)
                err_msg = 'SEARCH_MODEL_INDEXES has a field {0} for the model {1} that does not exist'.format(field_key, ModelKls)
                logging.error(err_msg)
    return model_fields