From c6d8f3bc7f1772e8d8eb3767436882ab172def91 Mon Sep 17 00:00:00 2001 From: Sagar Vora Date: Wed, 22 Jun 2022 06:43:35 +0000 Subject: [PATCH] fix: dont clear `_meta` when caching doc (#17115) fix: dont clear meta when caching doc --- frappe/model/base_document.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/frappe/model/base_document.py b/frappe/model/base_document.py index d3cc662d52..8e417bb45c 100644 --- a/frappe/model/base_document.py +++ b/frappe/model/base_document.py @@ -123,8 +123,23 @@ class BaseDocument(object): return meta def __getstate__(self): - self._meta = None - return self.__dict__ + """ + Called when pickling. + Returns a copy of `__dict__` excluding unpicklable values like `_meta`. + + More info: https://docs.python.org/3/library/pickle.html#handling-stateful-objects + """ + + # Always use the dict.copy() method to avoid modifying the original state + state = self.__dict__.copy() + self.remove_unpicklable_values(state) + + return state + + def remove_unpicklable_values(self, state): + """Remove unpicklable values before pickling""" + + state.pop("_meta", None) def update(self, d): """Update multiple fields of a doctype using a dictionary of key-value pairs.