Skip to content

Instantly share code, notes, and snippets.

@santiagomalter santiagomalter/example.py Secret
Created Feb 28, 2017

Embed
What would you like to do?
Matching score calculation
# Same role family
queryset = queryset.annotate(
same_role_family=Case(
When(target_role_families__in=[
user.role_family], then=Value(30)),
default=Value(0),
output_field=IntegerField()
)
)
# Same role category
queryset = queryset.annotate(
same_role_category=Case(
When(target_role_categories__in=[
user.role_category], then=Value(30)),
default=Value(0),
output_field=IntegerField()
)
)
# Same branch
queryset = queryset.annotate(
same_branch=Case(
When(
created_by__department__entity__branch=user.department.entity.branch, then=Value(10)),
default=Value(0),
output_field=IntegerField()
)
)
# Same entity
queryset = queryset.annotate(
same_entity=Case(
When(created_by__department__entity=user.department.entity,
then=Value(10)),
default=Value(0),
output_field=IntegerField()
)
)
# Same department
queryset = queryset.annotate(
same_department=Case(
When(created_by__department=user.department, then=Value(10)),
default=Value(0),
output_field=IntegerField()
)
)
# Match category
queryset = queryset.annotate(
match_category=Case(
When(category__in=user.interests.all(), then=Value(50)),
default=Value(0),
output_field=IntegerField()
)
)
# Sum
queryset = queryset.annotate(
relevancy_score=F('same_role_family') + F('same_role_category') +
F('same_branch') + F('same_entity') +
F('same_department') + F('match_category')
)
queryset = queryset.order_by('-relevancy_score')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.