@@ -19,6 +19,30 @@ class ConflictAction(Enum):
19
19
20
20
21
21
class PostgresQuery (sql .Query ):
22
+ def rename_annotations (self , annotations ) -> None :
23
+ """Renames the aliases for the specified annotations:
24
+
25
+ .annotate(myfield=F('somestuf__myfield'))
26
+ .rename_annotations(myfield='field')
27
+
28
+ Arguments:
29
+ annotations:
30
+ The annotations to rename. Mapping the
31
+ old name to the new name.
32
+ """
33
+
34
+ for old_name , new_name in annotations .items ():
35
+ annotation = self .annotations .get (old_name )
36
+
37
+ if not annotation :
38
+ raise SuspiciousOperation ((
39
+ 'Cannot rename annotation "{old_name}" to "{new_name}", because there'
40
+ ' is no annotation named "{old_name}".'
41
+ ).format (old_name = old_name , new_name = new_name ))
42
+
43
+ del self .annotations [old_name ]
44
+ self .annotations [new_name ] = annotation
45
+
22
46
def add_join_conditions (self , conditions : Dict [str , Any ]) -> None :
23
47
"""Adds an extra condition to an existing JOIN.
24
48
@@ -45,7 +69,7 @@ def add_join_conditions(self, conditions: Dict[str, Any]) -> None:
45
69
if not join :
46
70
raise SuspiciousOperation ((
47
71
'Cannot add an extra join condition for "%s", there\' s no'
48
- 'existing join to add it to.'
72
+ ' existing join to add it to.'
49
73
) % target_table )
50
74
51
75
# convert the Join object into a ConditionalJoin object, which
0 commit comments