Skip to content

Instantly share code, notes, and snippets.

@leonardoo
Created September 8, 2015 16:38
Show Gist options
  • Save leonardoo/96ba9255313688a1be47 to your computer and use it in GitHub Desktop.
Save leonardoo/96ba9255313688a1be47 to your computer and use it in GitHub Desktop.
this mixins convert a model to another, without take id field and only the fields that both models share
class ConvertMixin(object):
def convert(self, obj):
options = obj._meta
for field in sorted(options.get_fields()):
if self.attr_exists(field.name) and field.name not in obj.get_list_remove():
self.__setattr__(field.name,obj.__getattribute__(field.name))
def attr_exists(self, name):
return hasattr(self, name) or hasattr(self,"%s_id" % name)
def get_list_remove(self):
return ["id", "pk"]
@mariosanchezc
Copy link

very interesting to better explain me please ?? or any related post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment