From c1b24718e05ea474955777d7bc4d9d5634560cd5 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Fri, 17 Jan 2020 12:44:37 +0000 Subject: [PATCH] Cached ForeignObject.(local/foreign/reverse)_related_fields properties. --- django/db/models/fields/related.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 88bc0b5c15b..f269d9e17fb 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -616,21 +616,19 @@ class ForeignObject(RelatedField): related_fields.append((from_field, to_field)) return related_fields - @property + @cached_property def related_fields(self): - if not hasattr(self, '_related_fields'): - self._related_fields = self.resolve_related_fields() - return self._related_fields + return self.resolve_related_fields() - @property + @cached_property def reverse_related_fields(self): return [(rhs_field, lhs_field) for lhs_field, rhs_field in self.related_fields] - @property + @cached_property def local_related_fields(self): return tuple(lhs_field for lhs_field, rhs_field in self.related_fields) - @property + @cached_property def foreign_related_fields(self): return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)