<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _to_rest_includes(models, includes):
 """ Fetch the models to be included The includes should follow a few basic rules: * the include MUST not already be an array member of the included array (no dupes) * the include MUST not be the same as the primary data if the primary data is a single resource object (no dupes) * the include MUST not be an array member of the primary data if the primary data an array of resource objects (no dupes) Basically, each included array member should be the only instance of that resource object in the entire restified data. """


    included = []
    includes = includes or []

    if not isinstance(models, list):
        models = [models]

    for include in includes:
        for model in models:
            rel = getattr(model, include)

            if hasattr(rel, 'model') and rel.model:
                rel_models = [rel.model]
            elif hasattr(rel, 'models') and rel.models:
                rel_models = rel.models

            for rel_model in rel_models:
                if rel_model in models or rel_model in included:
                    continue
                else:
                    included.append(rel_model)

    for idx, val in enumerate(included):
        included[idx] = _to_rest(val)

    return included