Add LocalSite.objects.build_q, for optimized LocalSite querying.

Review Request #12355 — Created June 11, 2022 and submitted

Review Board

This introduces LocalSite.objects.build_q(), which builds a Q()
result based on the provided LocalSite/LocalSite.ALL value and
whether Local Site functionality is used in the deployment.

If either LocalSite.ALL is passed or Local Sites are not in use, the
result will be an empty Q(), which will be optimized out when joining
with another.

If Local Sites are used, this will construct a Q(local_site=...)

It also provides validation on arguments, ensuring that a LocalSite
instance cannot be accepted if the database does not contain any
Local Sites. This will mostly help us with writing correct tests and
to catch errors with stale caches.

This will greatly help us build queries in the most optimized way
possible. On normal deployments without Local Sites, these will be left
off entirely, allowing the database to make more optimized query plans.
Future changes will begin to introduce usage of this method.

Successfully made use of this in some in-progress changes, and
verified with those unit tests.

Add LocalSite.objects.build_q, for optimized LocalSite querying.
  1. Ship It!
Review request changed

Status: Closed (submitted)

Change Summary:

Pushed to release-5.0.x (8872035)