Used @cached_property in RawQuerySet.

This commit is contained in:
Adam Chainz 2016-12-23 15:12:48 +00:00 committed by Tim Graham
parent 6ebf8f9057
commit 8d94d575f8
1 changed files with 18 additions and 22 deletions

View File

@ -1241,38 +1241,34 @@ class RawQuerySet(object):
using=alias, using=alias,
) )
@property @cached_property
def columns(self): def columns(self):
""" """
A list of model field names in the order they'll appear in the A list of model field names in the order they'll appear in the
query results. query results.
""" """
if not hasattr(self, '_columns'): columns = self.query.get_columns()
self._columns = self.query.get_columns()
# Adjust any column names which don't match field names # Adjust any column names which don't match field names
for (query_name, model_name) in self.translations.items(): for (query_name, model_name) in self.translations.items():
try: try:
index = self._columns.index(query_name) index = columns.index(query_name)
self._columns[index] = model_name columns[index] = model_name
except ValueError: except ValueError:
# Ignore translations for non-existent column names # Ignore translations for non-existent column names
pass pass
return columns
return self._columns @cached_property
@property
def model_fields(self): def model_fields(self):
""" """
A dict mapping column names to model field names. A dict mapping column names to model field names.
""" """
if not hasattr(self, '_model_fields'):
converter = connections[self.db].introspection.table_name_converter converter = connections[self.db].introspection.table_name_converter
self._model_fields = {} model_fields = {}
for field in self.model._meta.fields: for field in self.model._meta.fields:
name, column = field.get_attname_column() name, column = field.get_attname_column()
self._model_fields[converter(column)] = field model_fields[converter(column)] = field
return self._model_fields return model_fields
class Prefetch(object): class Prefetch(object):