|
7 | 7 |
|
8 | 8 | class ProjectManager(Manager): |
9 | 9 | def get_projects_for_list_view(self): |
10 | | - return self.get_queryset().prefetch_related( |
11 | | - Prefetch( |
12 | | - "industry", |
13 | | - queryset=Industry.objects.only("name").all(), |
14 | | - ), |
15 | | - Prefetch( |
16 | | - "leader", |
17 | | - queryset=CustomUser.objects.only("id").all(), |
18 | | - ), |
19 | | - # Prefetch( |
20 | | - # "collaborator_set", |
21 | | - # queryset=Collaborator.objects.filter( |
22 | | - # id__in=Subquery( |
23 | | - # Collaborator.objects |
24 | | - # .filter(project_id=OuterRef('project_id')).values_list('id', flat=True)[:4] |
25 | | - # ) |
26 | | - # ), |
27 | | - # to_attr='collaborators' |
28 | | - # ), |
29 | | - # Yes, this fetches the entire collaborator_set even though we only need 4 and the total count. |
30 | | - # No, You can't do it any other way than this. |
31 | | - # Above is a hack that can fetch max 4 vacancies, but if you use it the count will always be <=4. |
32 | | - # To get the count right using the thing above you either have to make another godawful hack, |
33 | | - # Or override the default QuerySet to always ask the DB only count after all the filters. |
34 | | - # (ticket referring to the reason why you can't |
35 | | - # prefetch N items easily https://code.djangoproject.com/ticket/26780) |
36 | | - # (seems like in django 4.2.0 it'll be fixed but at the time of writing the latest version is 4.1.3 |
37 | | - Prefetch("collaborator_set"), |
| 10 | + return ( |
| 11 | + self.get_queryset() |
| 12 | + .filter(draft=False) |
| 13 | + .prefetch_related( |
| 14 | + Prefetch( |
| 15 | + "industry", |
| 16 | + queryset=Industry.objects.only("name").all(), |
| 17 | + ), |
| 18 | + Prefetch( |
| 19 | + "leader", |
| 20 | + queryset=CustomUser.objects.only("id").all(), |
| 21 | + ), |
| 22 | + # Prefetch( |
| 23 | + # "collaborator_set", |
| 24 | + # queryset=Collaborator.objects.filter( |
| 25 | + # id__in=Subquery( |
| 26 | + # Collaborator.objects |
| 27 | + # .filter(project_id=OuterRef('project_id')).values_list('id', flat=True)[:4] |
| 28 | + # ) |
| 29 | + # ), |
| 30 | + # to_attr='collaborators' |
| 31 | + # ), |
| 32 | + # Yes, this fetches the entire collaborator_set even though we only need 4 and the total count. |
| 33 | + # No, You can't do it any other way than this. |
| 34 | + # Above is a hack that can fetch max 4 vacancies, but if you use it the count will always be <=4. |
| 35 | + # To get the count right using the thing above you either have to make another godawful hack, |
| 36 | + # Or override the default QuerySet to always ask the DB only count after all the filters. |
| 37 | + # (ticket referring to the reason why you can't |
| 38 | + # prefetch N items easily https://code.djangoproject.com/ticket/26780) |
| 39 | + # (seems like in django 4.2.0 it'll be fixed but at the time of writing the latest version is 4.1.3 |
| 40 | + Prefetch("collaborator_set"), |
| 41 | + ) |
38 | 42 | ) |
39 | 43 |
|
40 | 44 | def get_projects_for_detail_view(self): |
41 | 45 | return ( |
42 | 46 | self.get_queryset().prefetch_related("achievements", "collaborator_set").all() |
43 | 47 | ) |
44 | 48 |
|
| 49 | + def get_projects_for_count_view(self): |
| 50 | + return self.get_queryset().filter(draft=False).only("id") |
| 51 | + |
45 | 52 | def check_if_owns_any_projects(self, user) -> bool: |
46 | 53 | # I don't think this should work but the function has no usages, so I'll let it be |
47 | 54 | return user.leader_projects.exists() |
|
0 commit comments