Skip to content

Instantly share code, notes, and snippets.

@JorgeMichelena
Created September 19, 2022 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JorgeMichelena/67571dd12d2319d58f2ae6f591a9001b to your computer and use it in GitHub Desktop.
Save JorgeMichelena/67571dd12d2319d58f2ae6f591a9001b to your computer and use it in GitHub Desktop.
has_matches = Q(num_matches__gt=0)
more_than_5_matches = Q(num_matches__gt=5)
same_zodiac_sign = Q(zodiac_sign=my_user.zodiac_sign)
has_matches_score = 5
more_than_5_matches_score = 5
same_zodiac_sign_score = 5
scored_users = User.objects.annotate(
num_matches=(
Count("targets__matches_1", distinct=True) + Count("targets__matches_2", distinct=True)
),
score=Case(
When(
has_matches & more_than_5_matches & same_zodiac_sign,
then=Value(
has_matches_score + more_than_5_matches_score + same_zodiac_sign_score
),
),
When(
has_matches & more_than_5_matches,
then=Value(has_matches_score + more_than_5_matches_score),
),
When(
has_matches & same_zodiac_sign,
then=Value(has_matches_score + same_zodiac_sign_score),
),
When(
has_matches,
then=Value(has_matches_score),
),
When(
same_zodiac_sign,
then=Value(same_zodiac_sign_score),
),
default=Value(0),
output_field=models.IntegerField(),
),
).order_by("-score")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment