python - How to cast model to instance of its descendant proxy model -


i have proxy model.

now cast given instance of parent model instance of proxy.

i have code in manager:

def from_parent_user(self, user):     instance = member()     single_fields = self.model._meta.fields     field in single_fields:         setattr(instance, field.name, getattr(user, field.name))      multiple_fields = self.model._meta.many_to_many     field in multiple_fields:         setattr(instance, field.name, getattr(user, field.name).all())     return instance 

this works making lot of queries when copying many many fields. worse doing this:

def from_parent_user(self, user):     return self.get(pk=user.pk) 

any way copy relationship foreing key, cache or , avoid making query @ all?

i using method in every request :/

edit:

this seems working:

def from_parent_user(self, user):     member = member()     single_fields = self.model._meta.fields     field in single_fields:         setattr(member, field.name, getattr(user, field.name))     multiple_fields = self.model._meta.many_to_many     field in multiple_fields:         # doing scares me, don't know if safe:         getattr(member, field.name).__dict__.update(getattr(user, field.name).__dict__)     return member 

if knows if doing many many managers safe or not, please leave comment/answer.

there few concerns have approach, after looking @ django's logic. employ similar you're doing, go top down approach simulating creation , association of field new parent model class.

concern 1:

if auto_created:     self.creation_counter = field.auto_creation_counter     field.auto_creation_counter -= 1 else:     self.creation_counter = field.creation_counter     field.creation_counter += 1 

here logic happens when field initialized, django increments static counter on field class. far can see, shouldn't affect negatively in bigger picture, , static reference find in field's __init__ method

concern 2:

def __deepcopy__(self, memodict):     # don't have deepcopy here, since things not     # intended altered after initial creation.     obj = copy.copy(self)     if self.rel:         obj.rel = copy.copy(self.rel)     memodict[id(self)] = obj     return obj 

this method found on field class, copy on relationship when field deep copied. inclined believe they've implemented level of copying specific reason, perhaps thwart off issues ran when doing similar , trying add bit of magic to. perhaps instead of copying dicts, perform deep copy , let django's deep copy implementation magic needs to.

aside 2 concerns, i've had success moving , copying fields new model class instances , see no "real" reason why way you've implemented should cause concern. if reason run relationship issue, @ least you'll know begin :)

edit

i've created gist illustrate full implementation: https://gist.github.com/bmoyles0117/5604915


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -