Changes between Initial Version and Version 1 of Ticket #34838, comment 2


Ignore:
Timestamp:
Sep 13, 2023, 2:18:53 PM (15 months ago)
Author:
Simon Charette

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34838, comment 2

    initial v1  
    11I suspect we'll want to avoid adding many `output_field = field; if field.generated: field.output_field` all over the codebase and that we should favour an approach where expression resolving (see `sql.query.Query.resolve_ref`) returns a `Col` with the proper `output_field`.
    22
    3 Maybe this can be done at the `GeneratedField.get_col` level?
     3Maybe this can be done at the `GeneratedField.get_col` level where the returned `Col` instance defaults to `output_field=self.output_field` instead of `self`.
     4
     5{{{#!diff
     6diff --git a/django/db/models/fields/generated.py b/django/db/models/fields/generated.py
     7index 0980be98af..948d11d003 100644
     8--- a/django/db/models/fields/generated.py
     9+++ b/django/db/models/fields/generated.py
     10@@ -48,6 +48,11 @@ def contribute_to_class(self, *args, **kwargs):
     11         for lookup_name, lookup in self.output_field.get_class_lookups().items():
     12             self.register_lookup(lookup, lookup_name=lookup_name)
     13
     14+    def get_col(self, alias, output_field=None):
     15+        if output_field is None:
     16+            output_field = self.output_field
     17+        return super().get_col(alias, output_field)
     18+
     19     def generated_sql(self, connection):
     20         return self._resolved_expression.as_sql(
     21             compiler=connection.ops.compiler("SQLCompiler")(
     22}}}
Back to Top