Skip to content

Instantly share code, notes, and snippets.

@LowerDeez
Created May 17, 2021 06:28
Show Gist options
  • Save LowerDeez/64234c2527b57ef01f08b67e2207e024 to your computer and use it in GitHub Desktop.
Save LowerDeez/64234c2527b57ef01f08b67e2207e024 to your computer and use it in GitHub Desktop.
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