Optimize transmogrify() function, assign __class__ instead.

fix_request_path_info
Diederik van der Boor 2013-04-07 22:26:31 +02:00
parent a0ab068449
commit bb0a4daddc
1 changed files with 11 additions and 4 deletions

View File

@ -19,13 +19,20 @@ Polymorphic_QuerySet_objects_per_request = CHUNK_SIZE
def transmogrify(cls, obj):
"""
Clone an object as a different class, by instantiating that class and copying the __dict__
Upcast a class to a different type without asking questions.
"""
new = cls()
for k,v in obj.__dict__.items():
new.__dict__[k] = v
if not '__init__' in obj.__dict__:
# Just assign __class__ to a different value.
new = obj
new.__class__ = cls
else:
# Run constructor, reassign values
new = cls()
for k,v in obj.__dict__.items():
new.__dict__[k] = v
return new
###################################################################################
### PolymorphicQuerySet