| 75 | |
| 76 | Using base_manager works to: |
| 77 | |
| 78 | |
| 79 | {{{#!python |
| 80 | Task._meta.base_manager.select_related(None).prefetch_related(None).filter(candidate=candidate, planning_rsc__isnull=False).only('pk').order_by('-planning_rsc', 'task_type_two') |
| 81 | }}} |
| 82 | {{{#!sql |
| 83 | SELECT "candidates_tasks"."id" |
| 84 | FROM "candidates_tasks" |
| 85 | WHERE ("candidates_tasks"."candidate_id" = 77677 AND "candidates_tasks"."planning_rsc" IS NOT NULL) |
| 86 | ORDER BY "candidates_tasks"."planning_rsc" DESC, "candidates_tasks"."task_type_two" ASC |
| 87 | }}} |
| 88 | |
| 89 | And |
| 90 | |
| 91 | {{{#!python |
| 92 | Task._meta.base_manager.select_related(None).prefetch_related(None).filter(candidate=candidate, planning_rsc__isnull=False).only('pk').order_by('task_type_two', '-planning_rsc').query) |
| 93 | }}} |
| 94 | {{{#!sql |
| 95 | SELECT "candidates_tasks"."id" |
| 96 | FROM "candidates_tasks" |
| 97 | WHERE ("candidates_tasks"."candidate_id" = 77677 AND "candidates_tasks"."planning_rsc" IS NOT NULL) |
| 98 | ORDER BY "candidates_tasks"."task_type_two" ASC, "candidates_tasks"."planning_rsc" DESC |
| 99 | }}} |
| 100 | |
| 101 | I cannot use **only** with **object** |
| 102 | |
| 103 | |
| 104 | {{{#!python |
| 105 | Task.objects.filter(candidate=candidate, planning_rsc__isnull=False).only('pk').order_by('-planning_rsc', 'task_type_two') |
| 106 | }}} |
| 107 | >django.core.exceptions.FieldError: Field Task.candidate cannot be both deferred and traversed using select_related at the same time. |
| 108 | |
| 109 | Then try with **values_list** |
| 110 | |
| 111 | |
| 112 | {{{#!python |
| 113 | Task.objects.filter(candidate=candidate, planning_rsc__isnull=False).values_list('pk').order_by('-planning_rsc', 'task_type_two') |
| 114 | }}} |
| 115 | {{{#!sql |
| 116 | SELECT "candidates_tasks"."id" |
| 117 | FROM "candidates_tasks" |
| 118 | LEFT OUTER JOIN "candidates_technologiestwo" |
| 119 | ON ("candidates_tasks"."id" = "candidates_technologiestwo"."task_id") |
| 120 | INNER JOIN "candidates_candidates" |
| 121 | ON ("candidates_tasks"."candidate_id" = "candidates_candidates"."location_ptr_id") |
| 122 | LEFT OUTER JOIN "iliad_operators" ON ("candidates_candidates"."landlord_operator_id" = "iliad_operators"."id") |
| 123 | LEFT OUTER JOIN "candidates_tower_companies" |
| 124 | ON ("candidates_candidates"."tower_company_id" = "candidates_tower_companies"."id") |
| 125 | INNER JOIN "iliad_locations" ON ("candidates_candidates"."location_ptr_id" = "iliad_locations"."id") |
| 126 | WHERE ("candidates_tasks"."candidate_id" = 77677 AND "candidates_tasks"."planning_rsc" IS NOT NULL) |
| 127 | GROUP BY "candidates_tasks"."id" |
| 128 | ORDER BY "candidates_tasks"."planning_rsc" DESC |
| 129 | }}} |
| 130 | |
| 131 | `task_type_two` is missing in **order_by** clause |