[Answer]-Optimizing model lookups in django

1👍

Using dict and dict unpacking:

def task_list(request, param=None, param_id=None):
    field_mapping = {'org': 'org__id', 'orgstaff': 'assigned_to__id', 'orgteam': 'team__id'}
    tasks = ToDo.objects.filter(**{field_mapping[param]: param_id}).exclude(todo_status=4)
    return render(request, "task_list.html", {"tasks":tasks})

Leave a comment