Created
May 17, 2021 06:28
-
-
Save LowerDeez/64234c2527b57ef01f08b67e2207e024 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import weakref | |
__refs__ = defaultdict(weakref.WeakSet) | |
def clear_refs(cls): | |
__refs__[cls].clear() | |
def get_refs(cls): | |
return __refs__[cls] | |
def set_ref(cls, obj): | |
if obj.pk: | |
__refs__[cls].add(obj) | |
def update_refs(cls, data): | |
for obj in get_refs(cls): | |
obj_key = str(obj.pk) | |
obj_data = data.get(obj_key) | |
if obj_data: | |
for key, value in obj_data.items(): | |
setattr(obj, key, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment