Created
September 8, 2015 16:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very interesting to better explain me please ?? or any related post