models: Replace __id syntax with _id where possible.

model__id syntax implies needing a JOIN on the model table to fetch the
id. That's usually redundant, because the first table in the query
simply has a 'model_id' column, so the id can be fetched directly.
Django is actually smart enough to not do those redundant joins, but we
should still avoid this misguided syntax.

The exceptions are ManytoMany fields and queries doing a backward
relationship lookup. If "streams" is a many-to-many relationship, then
streams_id is invalid - streams__id syntax is needed. If "y" is a
foreign fields from X to Y:
class X:
  y = models.ForeignKey(Y)

then object x of class X has the field x.y_id, but y of class Y doesn't
have y.x_id. Thus Y queries need to be done like
Y.objects.filter(x__id__in=some_list)
This commit is contained in:
Mateusz Mandera
2021-04-22 16:23:09 +02:00
committed by Tim Abbott
parent 11177a40da
commit 1a8ad796f8
12 changed files with 33 additions and 33 deletions

View File

@@ -1088,7 +1088,7 @@ def export_partial_message_files(
).values_list("id", flat=True)
consented_recipient_ids = Subscription.objects.filter(
user_profile__id__in=consented_user_ids
user_profile_id__in=consented_user_ids
).values_list("recipient_id", flat=True)
recipient_ids = set(public_stream_recipient_ids) | set(consented_recipient_ids)
@@ -1897,7 +1897,7 @@ def get_analytics_config() -> Config:
def get_consented_user_ids(consent_message_id: int) -> Set[int]:
return set(
Reaction.objects.filter(
message__id=consent_message_id,
message_id=consent_message_id,
reaction_type="unicode_emoji",
# outbox = 1f4e4
emoji_code="1f4e4",