Skip to content

Instantly share code, notes, and snippets.

@gamerlv
Last active December 10, 2015 08:28
Show Gist options
  • Save gamerlv/4408077 to your computer and use it in GitHub Desktop.
Save gamerlv/4408077 to your computer and use it in GitHub Desktop.
This is an simple stripped down example of how I would like to see an python ORM ( Or query builder, if that's closer to this ). No need for an exact copy, just something close would do.

My main point I need are:

  • Models need to be classes, instancesable and have either a save function or something like database.insert(model)
  • Column need to be acceble in a varible fasion: model.column (eg person.firstname)
  • Models need to beable to have custom methods

I've already looked at:

import sqlObject from mysqlORM
#The use of sqlObject here is just arbitairy, no affiliation to sqlObject was intended.
class person(sqlObject):
"""A singel person from the database"""
__table__='users'
def __init__(self, database):
super(person, self).__init__()
self.database = database
def fullname(self):
return self.firstname + ' ' + self.lastname
def activeProducts(self):
"""Return a ccollection of active product sqlObject's """
return self.products.active()
class products(sqlObject):
"""All products from the database"""
def __init__(self, database, limitor):
super(products, self).__init__()
self.database = database
self.database.appendWhere( limitor ) #just something fancy, not really needed, could also write some plain sql that is alwas appended
def active(self):
return self.database.select().where('active',True)
print person(1).activeProducts()
print person(name='steve').lastname
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment