예제 #1
0
 def insert(self, merge=True, return_key='id'):
     """Convenience method to add a model to the session
     and ultimately insert in the database permanently upon commit."""
     Session.add(self)
     if hasattr(self, 'id'):
         return json_dumps({return_key: self.id})
     else:
         return True
예제 #2
0
 def insert(self, merge=True, return_key='id'):
     """Convenience method to add a model to the session
     and ultimately insert in the database permanently upon commit."""
     Session.add(self)
     if hasattr(self, 'id'):
         return json_dumps({return_key: self.id})
     else:
         return True
예제 #3
0
    def to_json(self, columns=None):
        '''
        Convenience method to generate a JSON object from a model instance. It will automatically convert camel case keys
        to underscore equivalents.

        @param: columns: A list of (camelCased) columns that should be included in the result as keys.

        @return: A JSON object with all the columns of the model as keys and values.
        '''
        from json import loads  # We need this import because our json_loads doesn't throw exceptions!
        my_dict = self.to_dict(camel_case=True, columns=columns)
        for key in my_dict:
            try:
                loaded = loads(my_dict[key])
                my_dict[key] = loaded
            except (TypeError, ValueError):
                # Not JSON... keep going.
                pass
        return json_dumps(my_dict)
예제 #4
0
    def to_json(self, columns=None):
        '''
        Convenience method to generate a JSON object from a model instance. It will automatically convert camel case keys
        to underscore equivalents.

        @param: columns: A list of (camelCased) columns that should be included in the result as keys.

        @return: A JSON object with all the columns of the model as keys and values.
        '''
        from json import loads  # We need this import because our json_loads doesn't throw exceptions!
        my_dict = self.to_dict(camel_case=True, columns=columns)
        for key in my_dict:
            try:
                loaded = loads(my_dict[key])
                my_dict[key] = loaded
            except (TypeError, ValueError):
                # Not JSON... keep going.
                pass
        return json_dumps(my_dict)
예제 #5
0
def results_to_json(result_list):
    return json_dumps([result.to_dict(camel_case=True) for result in result_list])
예제 #6
0
def results_to_json(result_list):
    return json_dumps(
        [result.to_dict(camel_case=True) for result in result_list])