#!/usr/bin/python import sys from peewee import * db = SqliteDatabase('servertracker.sqlite') class BaseModel(Model): class Meta: database = db class System(BaseModel): systemid = IntegerField(unique=True,primary_key=True) hostname = CharField(unique=True,index=True,max_length=64) serial = CharField(max_length=64) cpus = IntegerField() memory = IntegerField() class Address(BaseModel): addressid = IntegerField(unique=True,primary_key=True) system = ForeignKeyField(System, related_name='addresses') address = CharField(unique=True, max_length=32) db.connect() # Just create the tables and exit #db.create_tables([System,Address,]) #sys.exit(0) # Examples s1 = System.select().where(System.hostname == 'testhost01').get() if not s1: s1 = System.create(hostname='testhost01', serial='', cpus=2, memory=4096) #a1 = Address.create(system=s1, address='10.1.1.2') s2 = System.select().where(System.hostname == 'testhost02').get() if not s1: s2 = System(hostname='testhost02', serial='', cpus=2, memory=4096) s2.save() #a2 = Address.create(system=s2, address='10.1.1.3') #a3 = Address.create(system=s2, address='10.1.1.4') # Selects for s in System.select(): print 'Host:', s.hostname for ip in s.addresses: print ' IP:', ip.address