python實現簡單的ORM

寫簡單的小項目這些挺方便的html

from peewee import *
from datetime import date
#http://docs.peewee-orm.com/en/latest/peewee/quickstart.html
db = SqliteDatabase('people.db')
class BaseModel(Model):
    class Meta:
        database = db # This model uses the "people.db" database.ui

class Person(BaseModel):
    name = CharField()
    birthday = DateField()
    is_relative = BooleanField()this

class Pet(Model):
    owner = ForeignKeyField(Person, backref='pets')
    name = CharField()
    animal_type = CharField()orm

    class Meta:
        database = db # this model uses the "people.db" database
    
"""
grandma = Person.get(Person.name == 'Grandma L.')
for person in Person.select():
    print(person.name, person.is_relative)
query = Pet.select().where(Pet.animal_type == 'cat')
for pet in query:
    print(pet.name, pet.owner.name)htm

for pet in Pet.select().where(Pet.owner == uncle_bob).order_by(Pet.name):
    print(pet.name)get

"""
if __name__=='__main__':
    print 'd'
    if 0:
        db.connect()
        db.create_tables([Person, Pet])
    
        uncle_bob = Person(name='Bob', birthday=date(1960, 1, 15), is_relative=True)
        uncle_bob.save() # bob is now stored in the database
        db.close()
    else:
        print Person.get(Person.name == 'Bob')
        for person in Person.select():
            print(person.name, person.is_relative)
        
# Returns: 1
    
 it

相關文章
相關標籤/搜索