def to_native(self, obj): model_name = obj.__class__.__name__.lower() # Try to get pk field # if not found, it's a child model # dependent on parent keys try: pk_field = primary_key(obj.__class__) pk_val = getattr(obj, pk_field, None) return self.parent_path + model_name + 's/' + str(pk_val) + '/' except KeyNotFoundException: # Use actual model name return self.parent_path + model_name + 's/'
def to_representation(self, value): model_name = value.__class__.__name__.lower() # Try to get pk field # if not found, it's a child model # dependent on parent keys try: pk_field = primary_key(value.__class__) pk_val = getattr(value, pk_field, None) return ('{parent}{model}s/{pk}/' ''.format(parent=self.parent_path, model=model_name, pk=pk_val)) except KeyNotFoundException: # Use actual model name return self.parent_path + model_name + 's/'