Optimize transmogrify() function, assign __class__ instead.
parent
a0ab068449
commit
bb0a4daddc
|
|
@ -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.
|
||||
"""
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue