Return SQL query for cleaned data

def get_query_parts(self):
        """
        Return SQL query for cleaned data
        """
        selects = []
        aliases = []
        froms = []
        wheres = []
        sorts = []
        groups_by = []
        params = []
        app_model_labels = None
        lookup_cast = self._db_operations.lookup_cast
        qn = self._db_operations.quote_name
        uqn = self._unquote_name
        for data in self.cleaned_data:
            if not ("model" in data and "field" in data):
                break
            model = data["model"]
            # HACK: Workaround to handle tables created
            #       by django for its own
            if not app_model_labels:
                app_models = get_models(include_auto_created=True,
                                        include_deferred=True)
                app_model_labels = [u"%s_%s" % (a._meta.app_label,
                                                a._meta.model_name)
                                    for a in app_models]
            if model in app_model_labels:
                position = app_model_labels.index(model)
                model = app_models[position]._meta.db_table
                self._models[model] = app_models[position]
            field = data["field"]
            show = data["show"]
            alias = data["alias"]
            criteria = data["criteria"]
            sort = data["sort"]
            group_by = data["group_by"]
            operator, over = criteria
            olower = operator.lower()
            if 'contains' in olower:
                over = '%' + over + '%'
            elif 'endswith' in olower:
                over = '%' + over
            elif 'startswith' in olower:
                over = over + '%'

            is_join = operator.lower() == 'join'
            db_field = self.get_db_field(model, field, qn, is_join=is_join)
            if show and not is_join:
                selects.append(db_field)
            if alias is not None and not is_join:
                aliases.append(alias)
            if sort:
                sorts.append(db_field + ('' if sort == 'asc' else ' DESC'))
            if group_by:
                groups_by.append(db_field)
            if all(criteria):
                if is_join:
                    over_split = over.lower().rsplit(".", 1)
                    join_model = qn(over_split[0].replace(".", "_"))
                    join_field = qn(over_split[1])
                    join = u"%s.%s = %s" % (join_model, join_field, db_field)
                    if (join not in wheres
                            and uqn(join_model) in self._db_table_names):
                        wheres.append(join)
                        if join_model not in froms:
                            froms.append(join_model)
                    # join_select = u"%s.%s" % (join_model, join_field)
                    # if join_select not in selects:
                    #     selects.append(join_select)
                elif operator in self._db_operators:
                    # db_operator = self._db_operators[operator] % over
                    db_operator = self._db_operators[operator]
                    lookup = self._get_lookup(operator, over)
                    params.append(lookup)
                    wheres.append(u"%s %s"
                                  % (lookup_cast(operator) % db_field,
                                     db_operator))
                elif operator in self._custom_operators:
                    CustOperator = self._custom_operators[operator]
                    custom_operator = CustOperator(db_field, operator, over)

                    # make sure the operators params are iterable:
                    custom_params = custom_operator.get_params()
                    if isinstance(custom_params, collections.Iterable):
                        params += custom_params
                    else:
                        params += [custom_params, ]

                    # make sure the operators wheres are iterable:
                    custom_wheres = custom_operator.get_wheres()
                    if isinstance(custom_wheres, collections.Iterable):
                        wheres += custom_wheres
                    else:
                        wheres += [custom_wheres, ]

            if qn(model) not in froms and model in self._db_table_names:
                froms.append(qn(model))
        return selects, aliases, froms, wheres, sorts, groups_by, params