Skip to content

Instantly share code, notes, and snippets.

@charettes
Created February 5, 2013 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charettes/4717921 to your computer and use it in GitHub Desktop.
Save charettes/4717921 to your computer and use it in GitHub Desktop.
Django #15363
def deprecated_get_query_set(cls):
def get_query_set(self, *args, **kwargs):
self.trace.append("%s.get_query_set" % cls.__name__)
return cls.get_queryset(self, *args, **kwargs)
cls.get_query_set = get_query_set
# Account for already defined __getattribute__
getattribute = getattr(cls, '__getattribute__')
def __getattribute__(self, name):
attribute = getattribute(self, name)
if name == 'get_queryset':
print(self, cls)
if attribute.im_func is cls.get_queryset.im_func:
# No defined `get_queryset` method
get_query_set = getattribute(self, 'get_query_set')
if get_query_set.im_func is not cls.get_query_set.im_func:
# Fallback to defined `get_query_set`
return get_query_set
return attribute
cls.__getattribute__ = __getattribute__
return cls
@deprecated_get_query_set
class Manager(object):
def __init__(self):
self.trace = []
def get_queryset(self):
self.trace.append('Manager.get_queryset')
return self.trace
class Old(Manager):
def get_query_set(self):
self.trace.append('Old.get_query_set')
return super(Old, self).get_query_set()
#assert Old().get_queryset() == ['Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset']
#assert Old().get_query_set() == ['Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset']
class OldSubclass(Manager):
def get_query_set(self):
self.trace.append('OldSubclass.get_query_set')
return super(OldSubclass, self).get_query_set()
#assert OldSubclass().get_queryset() == ['OldSubclass.get_query_set', 'Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset']
#assert OldSubclass().get_query_set() == ['OldSubclass.get_query_set', 'Old.get_query_set', 'Manager.get_query_set', 'Manager.get_queryset']
@deprecated_get_query_set
class New(Manager):
def get_queryset(self):
self.trace.append('New.get_queryset')
return super(New, self).get_queryset()
#assert New().get_queryset() == ['New.get_queryset', 'Manager.get_queryset']
#assert New().get_query_set() == ['New.get_query_set', 'New.get_queryset', 'Manager.get_queryset']
@deprecated_get_query_set
class NewSubclass(Manager):
def get_queryset(self):
self.trace.append('NewSubclass.get_queryset')
return super(NewSubclass, self).get_queryset()
assert NewSubclass().get_queryset() == ['NewSubclass.get_queryset', 'New.get_queryset', 'Manager.get_queryset']
assert NewSubclass().get_query_set() == ['NewSubclass.get_query_set', 'NewSubclass.get_queryset', 'New.get_queryset', 'Manager.get_queryset']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment