'If the update depends on results from other tables, munge the "where"
conditions to match the format required for (portable) SQL updates.
If multiple updates are required, pull out the id values to update at
this point so that they don\'t change as a result of the progressive
updates.'

def pre_sql_setup(self):

    refcounts_before = self.query.alias_refcount.copy()
    self.query.get_initial_alias()
    count = self.query.count_active_tables()
    if ((not self.query.related_updates) and (count == 1)):
        return
    query = self.query.chain(klass=Query)
    query.select_related = False
    query.clear_ordering(True)
    query._extra = {}
    query.select = []
    query.add_fields([query.get_meta().pk.name])
    super().pre_sql_setup()
    must_pre_select = ((count > 1) and (not self.connection.features.update_can_self_select))
    self.query.where = self.query.where_class()
    if (self.query.related_updates or must_pre_select):
        idents = []
        for rows in query.get_compiler(self.using).execute_sql(MULTI):
            idents.extend((r[0] for r in rows))
        self.query.add_filter(('pk__in', idents))
        self.query.related_ids = idents
    else:
        self.query.add_filter(('pk__in', query))
    self.query.reset_refcounts(refcounts_before)
