Manage multi-apps dependencies when squashing (#23733)

See https://code.djangoproject.com/ticket/23733, following https://code.djangoproject.com/ticket/23556