To switch from a model class to a proxy class without hitting the database:
class EntryProxy(Entry):
@property
def category(self):
new_inst = EntryProxy()
new_inst.__dict__ = super(EntryProxy, self).category.__dict__
return new_inst
edit: the snippet above seems not working on django 1.4.
Since django 1.4, I take all value fields manually like this:
class EntryProxy(Entry):
@property
def category(self):
category = super(EntryProxy, self).category
new_inst = EntryProxy()
for attr in [f.attname for f in category.__class__._meta.fields] + ['_state']:
setattr(new_inst, attr, getattr(category, attr))
return new_inst
To switch from a queryset to a child proxy class without hitting database:
class CategoryProxy(Category):
@property
def entry_set(self):
qs = super(CategoryProxy, self).entry_set
qs.model = EntryProxy
return qs
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…