parameters: (model, exclude_keys=None)
return_statement: return model_dict
docstring: Converts the given SQLAlchemy model into a dictionary, primarily used
      for serialization to JSON.
  
      - Includes columns, relationships, and properties decorated with
      `@property` (useful for calculated values from relationships).
      - Excludes the keys in `exclude_keys` and the keys in the given model's
      `exclude_keys` property or attribute.
      - Excludes any property or attribute with a leading underscore.
docstring_summary: Converts the given SQLAlchemy model into a dictionary, primarily used
      for serialization to JSON.
docstring_tokens: Converts the given SQLAlchemy model into a dictionary primarily used for serialization to JSON .
function: def model_to_dictionary(model, exclude_keys=None):
      """Converts the given SQLAlchemy model into a dictionary, primarily used
      for serialization to JSON.
  
      - Includes columns, relationships, and properties decorated with
      `@property` (useful for calculated values from relationships).
      - Excludes the keys in `exclude_keys` and the keys in the given model's
      `exclude_keys` property or attribute.
      - Excludes any property or attribute with a leading underscore.
      """
      model_dict = {}
  
      columns = model.__table__.columns.keys()
      relationships = model.__mapper__.relationships.keys()
      properties = []
      for key in list(set(dir(model)) - set(columns) - set(relationships)):
          if hasattr(type(model), key):
              attr = getattr(type(model), key)
              if not callable(attr) and isinstance(attr, property):
                  properties.append(key)
  
      if exclude_keys is None:
          exclude_keys = []
      if hasattr(model, "exclude_keys"):
          exclude_keys.extend(model.exclude_keys)
  
      assert set(exclude_keys).issubset(set(properties).union(columns))
  
      for key in columns:
          if key not in exclude_keys and not key.startswith("_"):
              model_dict[key] = getattr(model, key)
  
      for key in properties:
          if key not in exclude_keys and not key.startswith("_"):
              model_dict[key] = getattr(model, key)
  
      for key in relationships:
          if key not in exclude_keys and not key.startswith("_"):
              attr = getattr(model, key)
              if isinstance(attr, list):
                  model_dict[key] = query_result_to_list(attr)
              else:
                  model_dict[key] = model_to_dictionary(attr)
  
      return model_dict
function_tokens: def model_to_dictionary ( model , exclude_keys = None ) : model_dict = { } columns = model . __table__ . columns . keys ( ) relationships = model . __mapper__ . relationships . keys ( ) properties = [ ] for key in list ( set ( dir ( model ) ) - set ( columns ) - set ( relationships ) ) : if hasattr ( type ( model ) , key ) : attr = getattr ( type ( model ) , key ) if not callable ( attr ) and isinstance ( attr , property ) : properties . append ( key ) if exclude_keys is None : exclude_keys = [ ] if hasattr ( model , "exclude_keys" ) : exclude_keys . extend ( model . exclude_keys ) assert set ( exclude_keys ) . issubset ( set ( properties ) . union ( columns ) ) for key in columns : if key not in exclude_keys and not key . startswith ( "_" ) : model_dict [ key ] = getattr ( model , key ) for key in properties : if key not in exclude_keys and not key . startswith ( "_" ) : model_dict [ key ] = getattr ( model , key ) for key in relationships : if key not in exclude_keys and not key . startswith ( "_" ) : attr = getattr ( model , key ) if isinstance ( attr , list ) : model_dict [ key ] = query_result_to_list ( attr ) else : model_dict [ key ] = model_to_dictionary ( attr ) return model_dict